Package org.logevents.observers
Class FileLogEventObserver
- java.lang.Object
-
- org.logevents.core.AbstractFilteredLogEventObserver
-
- org.logevents.observers.FileLogEventObserver
-
- All Implemented Interfaces:
AutoCloseable
,LogEventObserver
- Direct Known Subclasses:
DateRollingLogEventObserver
public class FileLogEventObserver extends AbstractFilteredLogEventObserver implements AutoCloseable
Logs events to file. By default, FileLogEventObserver will log to the filelogs/your-app-name-%date.log
as determined bydefaultFilename(org.logevents.config.Configuration)
. By default, each log event is logged with time, thread, level and logger. UsesFileRotationWorker
to archive and delete old logs.Example configuration:
observer.file.filename=logs/%application.log observer.file.archivedFilename=logs/%date{%yyyy-MM}/%application-%date.log observer.file.retention=P4W observer.file.compressAfter=P1W observer.file.formatter=PatternLogEventFormatter observer.file.formatter.pattern=%date %coloredLevel: %msg observer.file.formatter.exceptionFormatter=CauseFirstExceptionFormatter observer.file.formatter.exceptionFormatter.packageFilter=sun.www,uninterestingPackage observer.file.threshold=WARN
File name pattern
The following conversion words are supported in the filename:-
%date
: The date and time of the log event. Optionally supports a date formatting pattern fromDateTimeFormatter.ofPattern(java.lang.String)
e.g. %date{DD-MMM HH:mm:ss}. Default format isyyyy-MM-dd HH:mm:ss.SSS
. %marker[{defaultValue}]
:Marker
(if any)-
%mdc{key:-default}
: the specifiedMDC
variable or default value if not set %application
: The value ofConfiguration.getApplicationName()
%node
: The value ofConfiguration.getNodeName()
()}
- See Also:
PatternLogEventFormatter
,FilenameFormatter
-
-
Constructor Summary
Constructors Constructor Description FileLogEventObserver(String filenamePattern, LogEventFormatter formatter)
FileLogEventObserver(Map<String,String> properties, String prefix)
FileLogEventObserver(Configuration configuration)
FileLogEventObserver(Configuration configuration, String filenamePattern, Optional<LogEventFormatter> formatter)
FileLogEventObserver(FileRotationWorker fileRotationWorker, LogEventFormatter formatter)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
static LogEventFormatter
createFormatter(Configuration configuration)
static String
defaultFilename(Configuration configuration)
Determines default log file name.protected void
doLogEvent(LogEvent logEvent)
protected Path
getFilename(LogEvent logEvent)
String
toString()
-
Methods inherited from class org.logevents.core.AbstractFilteredLogEventObserver
configureFilter, filteredOn, getCondition, getThreshold, isEnabled, logEvent, setCondition, setFilter, setThreshold, shouldLogEvent
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.logevents.LogEventObserver
isEnabled, stream, toList
-
-
-
-
Constructor Detail
-
FileLogEventObserver
public FileLogEventObserver(Configuration configuration)
-
FileLogEventObserver
public FileLogEventObserver(Configuration configuration, String filenamePattern, Optional<LogEventFormatter> formatter)
-
FileLogEventObserver
public FileLogEventObserver(String filenamePattern, LogEventFormatter formatter)
-
FileLogEventObserver
public FileLogEventObserver(FileRotationWorker fileRotationWorker, LogEventFormatter formatter)
-
-
Method Detail
-
createFormatter
public static LogEventFormatter createFormatter(Configuration configuration)
-
defaultFilename
public static String defaultFilename(Configuration configuration)
Determines default log file name. If the main class of the application was loaded from a jar-file, the name of the jar-file, without version number, will be used. If the main class of the application was loaded from a directory, the current working directory base name will be used.
-
doLogEvent
protected void doLogEvent(LogEvent logEvent)
- Specified by:
doLogEvent
in classAbstractFilteredLogEventObserver
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
-
-