use of java.util.logging.Formatter in project Payara by payara.
the class LoggerFactoryJDK14 method configureFileHandler.
/**
* This method throws SecurityException if a security manager exists and if
* the caller does not have <tt>LoggingPermission("control"))</tt> or the
* calling code is not placed in the doPrivileged() block.
*/
protected void configureFileHandler(LoggerJDK14 logger) {
String name = logger.getName();
// NOI18N
String baseName = name + ".FileHandler";
LogManager logManager = LogManager.getLogManager();
// NOI18N
String pattern = logManager.getProperty(baseName + ".pattern");
if (pattern != null) {
// If pattern != null, create and attach a FileHandler to logger.
// Look various properties . If not found, fall back to
// defaults
int defaultLimit = 0;
// NOI18N
String limit = logManager.getProperty(baseName + ".limit");
if (limit != null) {
try {
defaultLimit = Integer.parseInt(limit);
if (defaultLimit < 0)
defaultLimit = 0;
} catch (NumberFormatException e) {
}
}
int defaultCount = 1;
// NOI18N
String count = logManager.getProperty(baseName + ".count");
if (count != null) {
try {
defaultCount = Integer.parseInt(count);
if (defaultCount < 1)
defaultCount = 1;
} catch (NumberFormatException e) {
}
}
boolean defaultAppend = false;
// NOI18N
String append = logManager.getProperty(baseName + ".append");
if (append != null) {
defaultAppend = Boolean.valueOf(append).booleanValue();
}
FileHandler fileHandler = null;
try {
fileHandler = new FileHandler(pattern, defaultLimit, defaultCount, defaultAppend);
} catch (Exception e) {
MessageFormat messageFormat = new MessageFormat(getMessages().getString(// NOI18N
"errorlogger.filehandler.initialize.exception"));
getErrorLogger().log(Logger.WARNING, messageFormat.format(new String[] { name }), e);
}
if (fileHandler != null) {
// Initialize various attributes for the new fileHandler
// --Level
// NOI18N
String level = logManager.getProperty(baseName + ".level");
if (level != null) {
try {
fileHandler.setLevel(Level.parse(level));
} catch (IllegalArgumentException e) {
}
}
// --Formatter
Formatter defaultFormatter = null;
// Initialize various attributes for the new fileHandler
// NOI18N
String formatter = logManager.getProperty(baseName + ".formatter");
if (formatter != null) {
try {
Class clz = ClassLoader.getSystemClassLoader().loadClass(formatter);
defaultFormatter = (Formatter) clz.newInstance();
} catch (Exception e) {
// We got one of a variety of exceptions in creating the
// class or creating an instance.
// Drop through.
MessageFormat messageFormat = new MessageFormat(getMessages().getString("errorlogger.formatter.initialize.exception"));
getErrorLogger().log(Logger.WARNING, messageFormat.format(new String[] { name }), e);
}
}
if (defaultFormatter == null) {
defaultFormatter = new SimpleFormatter();
}
try {
fileHandler.setFormatter(defaultFormatter);
} catch (IllegalArgumentException e) {
}
logger.addHandler(fileHandler);
}
// if(fileHandler != null)
}
// if(pattern != null)
}
use of java.util.logging.Formatter in project Payara by payara.
the class GFFileHandler method publish.
/**
* Publishes the logrecord storing it in our queue
*/
@Override
public void publish(LogRecord record) {
// the queue has shutdown, we are not processing any more records
if (done.isSignalled()) {
return;
}
// JUL LogRecord does not capture thread-name. Create a wrapper to
// capture the name of the logging thread so that a formatter can
// output correct thread-name if done asynchronously. Note that
// this fix is limited to records published through this handler only.
// ***
// PAYARA-406 Check if the LogRecord passed in is already a GFLogRecord,
// and just cast the passed record if it is
GFLogRecord recordWrapper;
if (record.getClass().getSimpleName().equals("GFLogRecord")) {
recordWrapper = (GFLogRecord) record;
// Check there is actually a set thread name
if (recordWrapper.getThreadName() == null) {
recordWrapper.setThreadName(Thread.currentThread().getName());
}
} else {
recordWrapper = new GFLogRecord(record);
// set the thread id to be the current thread that is logging the message
recordWrapper.setThreadName(Thread.currentThread().getName());
}
try {
pendingRecords.add(recordWrapper);
} catch (IllegalStateException e) {
// queue is full, start waiting.
try {
pendingRecords.put(recordWrapper);
} catch (InterruptedException e1) {
// too bad, record is lost...
}
}
Formatter formatter = this.getFormatter();
if (!(formatter instanceof LogEventBroadcaster)) {
LogEvent logEvent = new LogEventImpl(record);
informLogEventListeners(logEvent);
}
}
use of java.util.logging.Formatter in project Payara by payara.
the class LogManagerService method getHandlerServices.
private Collection<Handler> getHandlerServices() {
Set<String> handlerServicesSet = new HashSet<String>();
handlerServicesSet.add(GFFileHandler.class.getName());
String[] handlerServicesArray = handlerServices.split(",");
for (String handlerService : handlerServicesArray) {
handlerServicesSet.add(handlerService);
}
List<Handler> handlers = habitat.getAllServices(Handler.class);
List<Handler> result = new ArrayList<Handler>();
List<Handler> customHandlers = new ArrayList<Handler>();
GFFileHandler gfFileHandler = null;
for (Handler handler : handlers) {
String handlerClassName = handler.getClass().getName();
if (handlerServicesSet.contains(handlerClassName)) {
result.add(handler);
}
if (handlerClassName.equals(GFFileHandler.class.getName())) {
gfFileHandler = (GFFileHandler) handler;
} else if (!handlerClassName.equals(PayaraNotificationFileHandler.class.getName())) {
customHandlers.add(handler);
}
}
// Set formatter on custom handler service if configured
for (Handler handler : customHandlers) {
try {
Map<String, String> props = getLoggingProperties();
String handlerClassName = handler.getClass().getName();
String formatterClassName = props.get(handlerClassName + ".formatter");
Formatter formatter = getCustomFormatter(formatterClassName, gfFileHandler);
if (formatter != null) {
handler.setFormatter(formatter);
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, LogFacade.ERROR_APPLYING_CONF, e);
}
}
return result;
}
use of java.util.logging.Formatter in project spring-roo by spring-projects.
the class HandlerUtils method wrapWithDeferredLogHandler.
/**
* Replaces each {@link Handler} defined against the presented
* {@link Logger} with {@link DeferredLogHandler}.
* <p>
* This is useful for ensuring any {@link Handler} defaults defined by the
* user are preserved and treated as the {@link DeferredLogHandler}
* "fallback" {@link Handler} if the indicated severity {@link Level} is
* encountered.
* <p>
* This method will create a {@link ConsoleHandler} if the presented
* {@link Logger} has no current {@link Handler}.
*
* @param logger to introspect and replace the {@link Handler}s for
* (required)
* @param fallbackSeverity to trigger fallback mode (required)
* @return the number of {@link DeferredLogHandler}s now registered against
* the {@link Logger} (guaranteed to be 1 or above)
*/
public static int wrapWithDeferredLogHandler(final Logger logger, final Level fallbackSeverity) {
Validate.notNull(logger, "Logger is required");
Validate.notNull(fallbackSeverity, "Fallback severity is required");
final List<DeferredLogHandler> newHandlers = new ArrayList<DeferredLogHandler>();
// Create DeferredLogHandlers for each Handler in presented Logger
final Handler[] handlers = logger.getHandlers();
if (handlers != null && handlers.length > 0) {
for (final Handler h : handlers) {
logger.removeHandler(h);
newHandlers.add(new DeferredLogHandler(h, fallbackSeverity));
}
}
// presented Logger
if (newHandlers.isEmpty()) {
final ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(new Formatter() {
@Override
public String format(final LogRecord record) {
return record.getMessage() + IOUtils.LINE_SEPARATOR;
}
});
newHandlers.add(new DeferredLogHandler(consoleHandler, fallbackSeverity));
}
// Add the new DeferredLogHandlers to the presented Logger
for (final DeferredLogHandler h : newHandlers) {
logger.addHandler(h);
}
return newHandlers.size();
}
use of java.util.logging.Formatter in project LuckPerms by lucko.
the class FileActionLogger method init.
public void init(File file) {
try {
FileHandler fh = new FileHandler(file.getAbsolutePath(), 0, 1, true);
fh.setFormatter(new Formatter() {
@Override
public String format(LogRecord record) {
return new Date(record.getMillis()).toString() + ": " + record.getMessage() + "\n";
}
});
this.actionLogger.addHandler(fh);
this.actionLogger.setUseParentHandlers(false);
this.actionLogger.setLevel(Level.ALL);
this.actionLogger.setFilter(record -> true);
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations