In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to use Log4j, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
# OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL or your defined level.
Log4j recommends using only four levels, and the priorities from high to low are ERROR, WARN, INFO, and DEBUG.
# the lowest level that can be displayed is defined here. If you define it to the INFO level, you will not see the DEBUG level information ~!
Log4j.rootLogger=DEBUG
# record the DAO layer log to DAOLog,allLog
Log4j.logger.DAO=DEBUG,A2,A4
# record the logical layer log to BusinessLog,allLog
Log4j.logger.Businesslog=DEBUG,A3,A4
# A1 color-print to the screen
Log4j.appender.A1=org.apache.log4j.ConsoleAppender
Log4j.appender.A1.layout=org.apache.log4j.PatternLayout
Log4j.appender.A1.layout.ConversionPattern=%-5p [% t]% 37c% 3x -% m% n
# A2Mel-print to file DAOLog-specifically for DAO layer
Log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
Log4j.appender.A2.file=DAOLog
Log4j.appender.A2.DatePattern='.'yyyy-MM-dd
Log4j.appender.A2.layout=org.apache.log4j.PatternLayout
Log4j.appender.A2.layout.ConversionPattern= [%-5p]% d {yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
# A3Mel-print to file BusinessLog-specifically record logic processing layer service log information
Log4j.appender.A3=org.apache.log4j.DailyRollingFileAppender
Log4j.appender.A3.file=BusinessLog
Log4j.appender.A3.DatePattern='.'yyyy-MM-dd
Log4j.appender.A3.layout=org.apache.log4j.PatternLayout
Log4j.appender.A3.layout.ConversionPattern= [%-5p]% d {yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
# A4Mel-print to file alllog-record all log information
Log4j.appender.A4=org.apache.log4j.DailyRollingFileAppender
Log4j.appender.A4.file=alllog
Log4j.appender.A4.DatePattern='.'yyyy-MM-dd
Log4j.appender.A4.layout=org.apache.log4j.PatternLayout
Log4j.appender.A4.layout.ConversionPattern= [%-5p]% d {yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
# use of Appender
Log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
Log4j.appender.A2.file=demo
Log4j.appender.A2.DatePattern='.'yyyy-MM-dd
Log4j.appender.A2.layout=org.apache.log4j.PatternLayout
Log4j.appender.A2.layout.ConversionPattern=%m%n
# configuration of Layout
Log4j.appender.A2.layout=org.apache.log4j.PatternLayout
Log4j.appender.A2.layout.ConversionPattern= [%-5p]% d {yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
The format meaning of ConversionPattern parameter
Meaning of format name
C output the full name of the class to which the log information belongs
% d output the date or time of the log point in time. The default format is ISO8601, or you can specify the format later, for example:% d {yyy-MM-dd HH:mm:ss}. The output is similar to: 2002-10-18-22:10:28
% f output the class name of the class to which the log information belongs
L output where the log event occurs, that is, the statement that outputs the log information is on the line of the class in which it is located
% m outputs the information specified in the code, such as message in log (message)
% n outputs a carriage return newline character with "rn" for Windows platform and "n" for Unix platform
% p output priority, which is DEBUG,INFO,WARN,ERROR,FATAL. DEBUG if it is output by calling debug (), and so on
% r outputs the number of milliseconds it took since the application was started to output the log information
T outputs the name of the thread that generated the log event
# 1 defines two outputs
Log4j.rootLogger = INFO, A1, A2mai A3
# 2 define A1 output to the controller
Log4j.appender.A1 = org.apache.log4j.ConsoleAppender
# 3 define the layout mode of A1 as PatternLayout
Log4j.appender.A1.layout = org.apache.log4j.PatternLayout
# 4 define the output format of A1
Log4j.appender.A1.layout.ConversionPattern =%-4r [% t]%-5p% c -% m% n
# 5 define A2 output to file
Log4j.appender.A2 = org.apache.log4j.RollingFileAppender
# 6 define which file A2 is to export to
Log4j.appender.A2.File = F:nepalonclassesexample3.log
# 7 define the maximum length of the output file of A2
Log4j.appender.A2.MaxFileSize = 1KB
# 8 define the number of backup files for A2
Log4j.appender.A2.MaxBackupIndex = 3
# 9 define the layout mode of A2 as PatternLayout
Log4j.appender.A2.layout = org.apache.log4j.PatternLayout
# 10 define the output format of A2
Log4j.appender.A2.layout.ConversionPattern =% d {yyyy-MM-dd hh:mm:ss}:% p% t% c -% m% n
Summary of configuring log4j
At this point in the tutorial, we have finished the basic principles of configuring the configuration file for log4j, and we can do the basic logging work through example 3. Now, let's make a summary. The basic steps to configure a configuration file are as follows:
1) define a Logger. Specify the level of the Logger and its output destination when defining the Logger. Define the format of Logger as
Log4j.rootLogger = [level], appendName1, appendName2, … AppendNameN .
2) define the output destination of appender. Define an output destination for appender in the format
Log4j.appender.appenderName = fully.qualified.name.of.appender.class.
The outputs provided by log4j are ConsoleAppender, FileAppender, DailyRollingFileAppender, RollingFileAppender and WriterAppender.
3) define other relevant parameters of appender in addition to the layout mode, such as example 3, 6, 7, 8 define the relevant parameters of A2. Define the format as
Log4j.appender.appenderName.optionName1 = value1
……
Log4j.appender.appenderName.optionNameN = valueN
If you do not need to define parameters other than layout mode, you can skip this step (such as A1 in example 3).
4) define the layout mode of appender. Define a layout pattern for appender in the format of
Log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class.
The layout mode is actually a part of step 3), except that the layout mode parameters are parameters that each appender must define. The layout modes provided by Log4j are HTMLLayout, PatternLayout, and SimpleLayout.
5) define the setting information related to the selected layout mode, in a format of
Og4j.appender.appenderName.layout.optionName1 = value1
……
Log4j.appender.appenderName.layout.optionNameN = valueN
When used in a class, import the following packages:
Import org.apache.log4j.BasicConfigurator
Import org.apache.log4j.Logger
Import org.apache.log4j.PropertyConfigurator
Create a log object in a class
Private Logger log = Logger.getLogger (this.getClass ())
The above is all the content of this article "how to use Log4j". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.