2020년 5월 11일 월요일

[Spring] Using log4j to collect logs

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

TagsDescription
<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 AttributeDescription
ConsoleAppenderOutputs a log message to the console.
FileAppenderOutputs a log message to a file.
RollingFileAppenderIf the file size exceeds a certain criteria, replace the baseline file with a backup file and rewrite it from the beginning.
DailyRollingAppenderAs 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.
AttributeDescription
%pLog level name
%mLog Message Output
%dWhen a Logging Event Occurs
%FProgram file name where logging occurred
%lInformation about the caller where logging occurred
%LNumber of lines on the caller where logging occurred
%cFull 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 LevellDescription
FATALThe level to be output when the application is not operational.
ERRORIndicates the status of a problem while the application is running.
WARNIndicates a warning message that may cause future system errors.
INFOIndicates information messages related to actual application operations, such as logins and status changes.
DEBUGIndicates the message used for debugging during development.
TRACELevel 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.

댓글 없음:

댓글 쓰기