Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch log snapshot from file path reference to UUID #93

Merged
merged 3 commits into from
Sep 18, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- This web script really should be a POST/DELETE, but needs to be GET to work with browser UI -->
<webscript>
<shortname>Log4J Snapshot</shortname>
<description>Finish log snapshot and shows it</description>
<url>/ootbee/admin/log4j-snapshot-complete/{path}</url>
<shortname>Log4J Settings - Log File - Close Snapshot</shortname>
<description>Closes (deregisters) a snapshot Log4J appender and retrieves the contents of the associated log file</description>
<url>/ootbee/admin/log4j-snapshots/{snapshotUUID}</url>
<authentication>admin</authentication>
<lifecycle>internal</lifecycle>
<transaction>none</transaction>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" ?>
<webscript>
<shortname>Log4J Settings - Log File - Create Snapshot</shortname>
<description>Accesses a specific Log4J log file</description>
<url>/ootbee/admin/log4j-snapshot-create</url>
<description>Creates a snapshot Log4J appender and log file</description>
<url>/ootbee/admin/log4j-snapshots</url>
<authentication>admin</authentication>
<format default="json"/>
<lifecycle>internal</lifecycle>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
* Copyright (C) 2005-2017 Alfresco Software Limited.
*/

model.snapshotLogFile = createSnapshot();
model.snapshotUUID = createSnapshot();
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Linked to Alfresco
Copyright (C) 2005-2017 Alfresco Software Limited.

-->

<#escape x as jsonUtils.encodeJSONString(x)>
{
"snapshotLogFile": "${snapshotLogFile}"
"snapshotUUID": "${snapshotUUID}"
}
</#compress>
</#escape></#compress>
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,9 @@ function getLoggersToSnapshot()
/* exported createSnapshot */
function createSnapshot()
{
var snapshotLogFile, logLayout, snapshotAppender, loggers;
var snapshotAppender, loggers;

snapshotLogFile = Packages.org.alfresco.util.TempFileProvider.createTempFile("ootbee-support-tools-snapshot", ".log");
logLayout = new Packages.org.apache.log4j.PatternLayout('%d{yyyy-MM-dd} %d{ABSOLUTE} %-5p [%c] [%t] %m%n');
snapshotAppender = new Packages.org.orderofthebee.addons.support.tools.repo.TemporaryFileAppender(logLayout, snapshotLogFile);
snapshotAppender = new Packages.org.orderofthebee.addons.support.tools.repo.TemporaryFileAppender('ootbee-support-tools-snapshot-');
loggers = getLoggersToSnapshot();
loggers.forEach(
function createSnapshot_connectLoggerAndAppender(logger)
Expand All @@ -457,19 +455,18 @@ function createSnapshot()
}
);

return snapshotLogFile;
return snapshotAppender.appenderUUID;
}

/* exported logSnapshotLapMessage */
function logSnapshotLapMessage(message) {
var root, clazz, level, lapLogger;
var lapLogger, level;

root = Packages.org.apache.log4j.Logger.getRootLogger();
// Fake logger that does not correspond to any class-based logger
lapLogger = Packages.org.apache.log4j.Logger.getLogger('org.orderofthebee.addons.support.tools.repo.logSnapshotLap');

// ensure level is enabled (in case someone reconfigured logger) and log
level = Packages.org.apache.log4j.Level.INFO;
// Fake logger that produces a good log message with the Alfresco default log format
clazz = 'org.orderofthebee.addons.support.tools.repo.logSnapshotLap';
lapLogger = root.getLogger(clazz);
lapLogger.setLevel(level);

lapLogger.log(level, message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var AdminLS = AdminLS || {};
var KEYCODE_ENTER = 13;
var KEYCODE_ESC = 27;

var serviceContext, snapshotLogFile, snapshotLapNumber;
var serviceContext, snapshotUUID, snapshotLapNumber;

AdminLS.setServiceContext = function setServiceContext(context)
{
Expand All @@ -58,13 +58,13 @@ var AdminLS = AdminLS || {};
AdminLS.startLogSnapshot = function startLogSnapshot()
{
Admin.request({
url : serviceContext + '/ootbee/admin/log4j-snapshot-create',
method : 'GET',
url : serviceContext + '/ootbee/admin/log4j-snapshots',
method : 'POST',
fnSuccess : function startLogSnapshot_success(res)
{
if (res.responseJSON)
{
snapshotLogFile = res.responseJSON.snapshotLogFile;
snapshotUUID = res.responseJSON.snapshotUUID;
document.getElementById("startLogSnapshot").style.display = 'none';
document.getElementById("stopLogSnapshot").style.display = 'inline';
document.getElementById("lapLogSnapshot").style.display = 'inline';
Expand All @@ -78,11 +78,12 @@ var AdminLS = AdminLS || {};

AdminLS.stopLogSnapshot = function stopLogSnapshot()
{
window.open(serviceContext + '/ootbee/admin/log4j-snapshot-complete/'+snapshotLogFile+'?a=true','_blank');
window.open(serviceContext + '/ootbee/admin/log4j-snapshots/' + encodeURIComponent(snapshotUUID) + '?a=true', '_blank');
document.getElementById("startLogSnapshot").style.display = 'inline';
document.getElementById("stopLogSnapshot").style.display = 'none';
document.getElementById("lapLogSnapshot").style.display = 'none';
document.getElementById("lapMessageLogSnapshot").style.display = 'none';
snapshotUUID = null;
};

AdminLS.lapLogSnapshot = function lapLogSnapshot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,29 @@
*/
package org.orderofthebee.addons.support.tools.repo;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.alfresco.util.TempFileProvider;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;

/**
* A FileAppender that will auto-close and unregister itself after 20 minutes.
*
* @author Bindu Wavell <a href="mailto:[email protected]">[email protected]</a>
*/
public class TemporaryFileAppender extends FileAppender
{

// 20 minutes after logger is created, we assume the UI lost access and automatically deregister this appender
private static final long AUTO_DEREGISTRATION_TIMEOUT = 1000 * 60 * 20;

Expand All @@ -48,17 +52,62 @@ public class TemporaryFileAppender extends FileAppender

protected final long creationTimestamp;

public
TemporaryFileAppender(Layout layout, String filename) throws IOException
protected final String appenderUUID = UUID.randomUUID().toString();

/**
* This default constructor transparently initialises the appender to use a file path provided by the Alfresco
* {@link TempFileProvider} and a pattern consistent with the default log pattern in the Alfresco-provided {@code log4j.properties}
* configuration.
*
* @throws IOException
* if the parent class constructor fails to create a file for the temporary file path
*/
public TemporaryFileAppender() throws IOException
{
this(new PatternLayout("%d{ISO8601} %-5p [%c] [%t] %m%n"),
TempFileProvider.createTempFile("ootbee-support-tools-", ".log").getPath());
}

/**
* This constructor initialises the appender to use a specific file name prefix for the log file path provided by
* the Alfresco {@link TempFileProvider}. The log pattern is transparently initialised with the default log pattern in the
* Alfresco-provided {@code log4j.properties} configuration.
*
*
* @param fileNamePrefix
* the file name prefix for the log file
* @throws IOException
* if the parent class constructor fails to create a file for the temporary file path
*/
public TemporaryFileAppender(final String fileNamePrefix) throws IOException
{
this(new PatternLayout("%d{ISO8601} %-5p [%c] [%t] %m%n"), fileNamePrefix);
}

/**
* This constructor initialises the appender to use a specific log message layout and file name prefix for the log file path provided by
* the Alfresco {@link TempFileProvider}.
*
*
* @param layout
* the layout for the messages written by this appender
* @param fileNamePrefix
* the file name prefix for the log file
* @throws IOException
* if the parent class constructor fails to create a file for the temporary file path
*/
public TemporaryFileAppender(final Layout layout, final String fileNamePrefix) throws IOException
{
super(layout, filename);
super(layout, TempFileProvider.createTempFile(fileNamePrefix, ".log").getPath());
this.creationTimestamp = System.currentTimeMillis();
}

/**
* <p>Assuming we have not timed out, does the usual. If we have timed out then deregisters
* <p>
* Assuming we have not timed out, does the usual. If we have timed out then deregisters
* appender from previously registered loggers and closes the appender. If the appender is
* closed then the append is simply ignored (parent would log a warning.)</p>
* closed then the append is simply ignored (parent would log a warning.)
* </p>
*
* {@inheritDoc}
*/
Expand All @@ -69,18 +118,22 @@ public void append(final LoggingEvent event)
if (active)
{
super.append(event);
} else {
}
else
{
if (!this.closed)
{
if (closingLock.tryLock())
if (this.closingLock.tryLock())
{
try
{
LogLog.warn("Automatically deregistering " + this.fileName + " appender after timeout exceeded.");
this.removeAppenderFromLoggers();
this.close();
} finally {
closingLock.unlock();
}
finally
{
this.closingLock.unlock();
}
}
// If we can't grab the closing lock we can assume another thread is
Expand All @@ -92,6 +145,9 @@ public void append(final LoggingEvent event)
/**
* Add this appender to a logger and then remember the logger so we can remove ourselves from
* all registered loggers when we are done.
*
* @param logger
* the logger to which to append this appender
*/
public void registerAsAppender(final Logger logger)
{
Expand All @@ -109,11 +165,21 @@ public void removeAppenderFromLoggers()
{
synchronized (this.appendedToLoggers)
{
for (Logger logger : this.appendedToLoggers)
for (final Logger logger : this.appendedToLoggers)
{
logger.removeAppender(this);
}
this.appendedToLoggers.clear();
}
}

/**
* Retrieves the UUID of this appender instance for identification independent of the file path.
*
* @return the appender UUID
*/
public String getAppenderUUID()
{
return this.appenderUUID;
}
}
Loading