File appender writes log messages to a
file. Following table explain the parameters of File appender.
Parameter
|
Type
|
Description
|
append
|
boolean
|
If set to true, records are appended
to end of the file, else file will be cleared before writing new log
messages.
|
bufferedIO
|
boolean
|
If it is set to true, records are
written to a buffer and the data will be written to disk when the buffer is
full or, if immediateFlush is set, when the record is written. Default value
is true.
|
bufferSize
|
Int
|
Size of the buffer. Default value is
8192 bytes.
|
filter
|
Filter
|
Filters are used to filter the log
events. Filters take the decision, whether to log the event (or) not. More
than one Filter may be used by using a CompositeFilter.
|
fileName
|
String
|
Name of the file to write to. If the
file (or), any of the parent directories do not exist, they will be created.
|
immediateFlush
|
boolean
|
Default value is true. If it is set to
true, each write will be followed by a flush.
|
layout
|
Layout
|
The Layout to use to format the
LogEvent.
|
locking
|
boolean
|
When set to true, I/O operations will
occur only while the file lock is held allowing FileAppenders in multiple
JVMs and potentially multiple hosts to write to the same file simultaneously.
Default value is false.
|
ignoreExceptions
|
boolean
|
If set to false, exception are
propagated to the caller, else exceptions encountered while appending events
to be internally logged and then ignored. Default value is true.
|
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="DEBUG" monitorInterval="30"> <Appenders> <Console name="my_console_appender" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> <File name="my_file_appender" fileName="/Users/harikrishna_gurram/application.log"> <PatternLayout> <Pattern>%d %p %c{1.} [%t] %m%n</Pattern> </PatternLayout> </File> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="my_console_appender" /> <AppenderRef ref="my_file_appender" /> </Root> </Loggers> </Configuration>
package log4j_tutorial; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class HelloWorld { private static final Logger logger = LogManager.getLogger(); public static void main(String args[]) { logger.trace("Trace Message!"); logger.debug("Debug Message!"); logger.info("Info Message!"); logger.warn("Warn Message!"); logger.error("Error Message!"); logger.fatal("Fatal Message!"); } }
Run
HelloWorld application, you can see following messages in /Users/harikrishna_gurram/application.log
file.
2015-10-03
19:51:58,258 INFO l.HelloWorld [main] Info Message!
2015-10-03
19:51:58,259 WARN l.HelloWorld [main] Warn Message!
2015-10-03
19:51:58,259 ERROR l.HelloWorld [main] Error Message!
2015-10-03 19:51:58,259 FATAL l.HelloWorld [main] Fatal Message!
2015-10-03 19:51:58,259 FATAL l.HelloWorld [main] Fatal Message!
No comments:
Post a Comment