Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of logs in Java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the example analysis of the log in Java, 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 go to know it!

Log4j

The full name of Log4j is Log for java, which is a logging tool dedicated to the Java language.

Log level

In order to facilitate the output display of the log information, the log content is graded and managed. Log levels range from high to low, with a total of 6 levels:

Fatal (fatal)

Error

Warn

Info

Debug

Trace (stack)

Log property profile

The log properties file log4j.properties is specifically used to control log output. It is mainly controlled in three aspects:

Output location: controls the location where the log will be output, whether it is a console or a file, etc.

Output layout: controls how the log information is displayed.

Output level: controls the log level to output.

The log properties file consists of two objects: the log appender and the root log.

The root log, the logger in the Java code, consists of two main attributes: the log output level and the log appender.

The log appender is defined by the log output location and decorated by many other attributes, such as output layout, file location, file size, and so on.

The so-called log append is to attach a lot of other setting information to the logger. The essence of an append is an interface whose definition syntax is: log4j.appender.appenderName = output location

Commonly used appendage implementation classes

Org.apache.log4j.ConsoleAppender: log output to console

Org.apache.log4j.FileAppender: log output to file

Org.apache.log4j.RollingFileAppender: a new log file will be generated when the log file size reaches the specified size

Org.apache.log4j.DailyRollingFileAppender: generate a log file every day

Org.apache.log4j.HTMLLayout: page layout, layout in HTML table form

Org.apache.log4j.SimpleLayout: simple layout, containing the level of log information and information string

Org.apache.log4j.PatternLayout: matcher layout, you can flexibly specify the layout mode. It is mainly through setting

Output in ConversionPattern format

The value of the ConversionPattern property of PatternLayout to control the specific output format.

Print parameters: Log4J formats log information in a print format similar to the printf function in C language

% m: output the message specified in the code

% p: output priority, i.e. DEBUG,INFO,WARN,ERROR,FATAL

% r: output the number of milliseconds it took since the application was started to output the log information

% c: the category to which the output belongs, which is usually the full name of the class

% t: output the name of the thread that generated the log event

% n: output a carriage return newline character, Windows platform is / rUnix platform is / n

% d: date or time of the output log point in time. The default format is ISO8601, or you can specify the format later, for example:% d {yyy MMM dd HH:mm:ss, SSS}. The output is similar: October 18, 2002. 22 SSS 10. 28921.

% l: where the output log event occurs, including the class name, the thread that occurred, and the number of lines in the code. For example: Testlog4.main (TestLog4.java: 10)

Create a property profile named log4j.properties in the src/main/resources directory

Log configuration instructions:

Log4j.rootLogger: root log, configured with log level of INFO, and predefined two appenders named console and file

Log4j.appender.console:console appender, log output is located in the console

Log4j.appender.console.layout:console appendage, using matcher layout mode

Log4j.appender.console.layout.ConversionPattern:console append, log output format is: date log level [class name]-message newline character

Log4j.appender.file:file appendage, which generates a log file every day

Log4j.appender.file.File:file appender, log file output location logs/log.log

Log4j.appender.file.layout:file appendage, using matcher layout mode

Log4j.appender.A3.MaxFileSize: log file maximum

Log4j.appender.A3.MaxBackupIndex: maximum number of files recorded

Log4j.appender.file.layout.ConversionPattern:file append, log output format is: date log level [class name]-message newline character

Slf4j

The full name of slf4j is Simple Loging Facade For Java, that is, it is only a unified interface to provide log output for Java programs, not a specific log implementation, such as JDBC, just a rule. Therefore, slf4j alone does not work and must be matched with other specific logging implementations.

Placeholder description

And remember, before producing the string of the final log information, this method checks to see if a specific log level is turned on, which not only reduces memory consumption but also reduces the time CPU takes to process string concatenation commands in advance.

Log naming and retention period

The log specification is also introduced in the public Alibaba java development manual. Here, the author introduces two of them, namely the log naming method and the log retention period.

How to name the log:

AppName represents the application name; logType represents the log type, and stats,monitor,visit is recommended for classification; and logName is the log description. The advantage of this naming is that you can quickly and clearly understand the type and purpose of the log file, and it is easy to classify and find.

Log retention period:

How to determine the log retention period is a thorny problem, if the log storage time is too long, it will consume a lot of storage resources, and even cause excessive disk pressure to affect the stability of the system; if the log storage time is too short, it may lead to the "loss" of log data, which cannot be traced when a problem occurs. Ali java development manual recommends that log files should be kept for at least 15 days. In practical application, it can be adjusted according to the importance, file size and disk space of the log file. in addition, the log can be monitored and dumped regularly.

The above is all the contents of the article "sample Analysis of logs in Java". 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report