public final class SimpleLogger extends Logger implements Closeable
SimpleLoggerSettings
and optionally your own
SimpleLoggerEvents
. To initialize the logger call the static method
getLogger(SimpleLoggerSettings)
(see code snippet 1 below).FileHandler
with a SimpleFormatter
for all developer level logging, a
FileHandler
with a SimpleLogger.SingleLineFormatter
for all user level logging, and a ConsoleHandler
for
either user or developer log activity when SimpleLoggerSettings.getLog2Console()
!= SimpleLogger.ConsoleLevel.OFF
.
To override this behavior you can close, remove, and/or add new handlers in your implementation of
SimpleLoggerEvents.onPostInitialization(SimpleLogger)
via the methods Logger.getHandlers()
,
Handler.close()
, Logger.removeHandler(Handler)
, and Logger.addHandler(Handler)
(see code snippet 2 below).FileHandler
, you can enable rolling logs by specifying a max size you want your log
file to grow to and the number of files to save(roll) in SimpleLoggerSettings
public class SingletonAppLogger { private static SimpleLogger logger = null; private static final String LOGGER_NAME = SingletonAppLogger.class.getCanonicalName(); private static final String CLASS_NAME = SingletonAppLogger.class.getSimpleName(); public static SimpleLogger getLogger() { if (logger == null) { logger = SimpleLogger.getLogger(new SingletonAppLoggerSettings()); } return logger; } private static class SingletonAppLoggerEvents implements SimpleLoggerEvents { public void onPreInitialization(SimpleLogger logger) { //TODO :: any pre initialization customization you want to define goes here } public void onPostInitialization(SimpleLogger logger) { //TODO :: any post initialization customization you want to define goes here } public void onLog(LogRecord record) { //TODO :: Use this method if you want to peek at the log record and take // some sort of action before the log event is forwarded to all registered log handlers } } private static class SingletonAppLoggerSettings extends SimpleLoggerSettings { //Create a logger for this application that directs user level output to app.log and //developer level output to app-detailed.log. Also, register our SingletonAppLoggerEvents with //this logger. Utilize the defaults in SimpleLoggerSettings for all other settings. public SingletonAppLoggerSettings() { super(); this.setLoggerName(LOGGER_NAME); this.setClassName(CLASS_NAME); this.setUserLogFileName("app.log"); this.setDeveloperLogFileName("app-detailed.log"); this.setSimpleLoggerEventsImpl(new SingletonAppLoggerEvents()); } }Snippet 2
...... public void onPostInitialization(SimpleLogger logger) { //Swap out the SimpleFormatter on the developer log FileHandler with an XMLFormatter Handler[] handlers = logger.getHandlers(); if (handlers != null) { for (Handler element : handlers) { if ((element.getFormatter().getClass().getSimpleName().matches("SimpleFormatter")) && (element.getClass().getSimpleName().matches("FileHandler")) && (element.getLevel() == logger.getLoggerSettings().getDevLevel())) { try { element.setFormatter(new XMLFormatter()); } catch (SecurityException e) { e.printStackTrace(); } } } } } ........
Modifier and Type | Class and Description |
---|---|
static class |
SimpleLogger.ConsoleLevel
This enum class represents the three options for console logging.
|
class |
SimpleLogger.SingleLineFormatter
Simple formatter class to produce terse, single line output
|
global, GLOBAL_LOGGER_NAME
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes all open log handlers.
|
void |
config(String msg) |
void |
entering()
Function entry log convenience method.
|
void |
entering(Object... params)
Function entry log convenience method (varargs-style).
|
void |
entering(Object param)
Function entry log convenience method with additional parm.
|
void |
exiting()
Function exit log convenience method.
|
void |
exiting(Object... params)
Function exit log convenience method (varargs-style).
|
void |
exiting(Object param)
Function exit log convenience method.
|
void |
fine(String msg) |
void |
finer(String msg) |
void |
finest(String msg) |
static SimpleLogger |
getLogger(SimpleLoggerSettings logSettings)
Find or create a new logger of type
SimpleLogger utilizing the settings specified via
SimpleLoggerSettings .If a new logger is created, log levels will be configured based on the SimpleLoggerSettings configuration
and it will also be configured to send logging output to parent handlers. |
SimpleLoggerSettings |
getLoggerSettings() |
void |
info(String msg) |
void |
log(Level level,
String msg) |
void |
log(Level level,
String msg,
Object param) |
void |
log(Level level,
String msg,
Object[] params) |
void |
log(Level level,
String msg,
Throwable thrown) |
void |
log(LogRecord record) |
void |
severe(String msg) |
static Level |
string2Level(String logLevelString)
Turns level string into
Level |
void |
warning(String msg) |
addHandler, config, entering, entering, entering, exiting, exiting, fine, finer, finest, getAnonymousLogger, getAnonymousLogger, getFilter, getGlobal, getHandlers, getLevel, getLogger, getLogger, getName, getParent, getResourceBundle, getResourceBundleName, getUseParentHandlers, info, isLoggable, log, log, logp, logp, logp, logp, logp, logp, logrb, logrb, logrb, logrb, logrb, logrb, removeHandler, setFilter, setLevel, setParent, setResourceBundle, setUseParentHandlers, severe, throwing, warning
public static SimpleLogger getLogger(SimpleLoggerSettings logSettings)
SimpleLogger
utilizing the settings specified via
SimpleLoggerSettings
.SimpleLoggerSettings
configuration
and it will also be configured to send logging output to parent handlers. Lastly, it will be registered in the
LogManager
global namespace.logSettings
- - the SimpleLoggerSettings
to applySimpleLogger
instancepublic static Level string2Level(String logLevelString)
Level
public SimpleLoggerSettings getLoggerSettings()
SimpleLoggerSettings
for this loggerpublic void entering()
public void entering(Object param)
param
- additional parampublic void entering(Object... params)
params
- varargspublic void exiting()
public void exiting(Object param)
param
- return valuepublic void exiting(Object... params)
params
- return valuespublic void close()
close
in interface Closeable
close
in interface AutoCloseable
Copyright © 2016 PayPal Open Source. All rights reserved.