What is log4j?
log4j is a library that provides log-related functions. For maintenance purposes, web applications store log files of information, such as user information that is accessed to the website or the time when a method is called, and log4j is used to collect logs.
Setting log4j
The log4j.xml file allows you to set up settings for the log, but there are some things you need to know before you do.
log4j tag
Tags | Description |
<appender> | Determines the output location of the log. |
<layout> | Sets the format in which the log is output |
<logger> | Pass the logging message to the appender. |
appender Class
You can set the location and method of log collection by specifying the class properties of the appender tag.
appender Class Attribute | Description |
ConsoleAppender | Outputs a log message to the console. |
FileAppender | Outputs a log message to a file. |
RollingFileAppender | If the file size exceeds a certain criteria, replace the baseline file with a backup file and rewrite it from the beginning. |
DailyRollingAppender | As a class, you create a new file in a set period of time and record a log message. |
Each property has the following meaning, and the log is output formatted into the value property of the param tag in the layout tag.
Attribute | Description |
%p | Log level name |
%m | Log Message Output |
%d | When a Logging Event Occurs |
%F | Program file name where logging occurred |
%l | Information about the caller where logging occurred |
%L | Number of lines on the caller where logging occurred |
%c | Full package name or full file name before logging message |
Log Level
The log level consists of a total of six steps, and the lower the table, the lower the level.
Log Levell | Description |
FATAL | The level to be output when the application is not operational. |
ERROR | Indicates the status of a problem while the application is running. |
WARN | Indicates a warning message that may cause future system errors. |
INFO | Indicates information messages related to actual application operations, such as logins and status changes. |
DEBUG | Indicates the message used for debugging during development. |
TRACE | Level introduced to output more detailed logging information than DEBUG level. |
If you set the reference level to a lower level in the log.xml file, all messages that you set above that level will be output. If set to info, all false, error, and warn level messages are output.
log4j.xml
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<!-- Application Loggers -->
<logger name="com.example.spring01">
<level value="info" />
</logger>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
</logger>
<logger name="org.springframework.beans">
<level value="info" />
</logger>
<logger name="org.springframework.context">
<level value="info" />
</logger>
<logger name="org.springframework.web">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
If you want to change the format of the log in the xml file, you can change the value property value of the param tag in the layout tag. Also, if you look at the application loggers in annotation, we set the reference log level using the logger tag.
No comments:
Post a Comment