Log4j notes, log4j Configuration
Log4j notes
I,Overview:
1.Log4j Introduction: Apache open-source project, used to manage and print output log information.
Logger + Appender + Layout
1. Logger: Logger, a program for generating and recording logs
2. Appender: logstore location, console/file/GUI component, etc. A Logg
1. Go to the official Download log4j 2, import jar package, basically you only need to import the following two jar packages on it (xx is a messy version number):
Log4j-core-xx.jar
Log4j-api-xx.jar
2. Import into your project: This will not be said.
3. Start using:
We know that to use the log4j log in a class, you
Set the directory by yourself, that is, set through system. setproperty at project startup to implement servletcontextlistener:
Public class log4jlistener implements servletcontextlistener {
Public static final string log4jdirkey = "log4jdir ";
Public void contextdestroyed (servletcontextevent ){
System. getproperties (). Remove (log4jdirkey );
}
Public void contextinitialized (servletcontextevent ){
String log4jdir = servletcontextevent. getservletcontext (). getrealpath ("/");
// System. Out
[Problem description] Configure the log, use slf4j and log4j together, and use c3p0 in the database connection pool to start the server. The following error occurs:Warning: Log4j: warn no appenders cocould be found for logger (COM. mchange. v2.log. mlog). log4j: Warn please initialize the log4j system properly. 650)
Dynamic configuration of log4j in Java Web and dynamic log4j in Java Web
Import the jar package of log4j,
Configure the following in web. xml:
Create the file log4j. properties in the WEB-INF
The content is as follows:
log4j.rootLogger=INFO, stdout, logfilelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.ap
1. Obtain log4j. Jar
Download address of log4j.zip: http://labs.mop.com/apache-mirror/logging/log4j/1.2.17/log4j-1.2.17.zip
Decompress log4j.zip to get the log4j-1.2.17.jar
2. Dependency log4j-1.2.17.jar in eclipse
Right-click Project -- Build path -- configure buil
name of the output category, the maximum width is 30, if the category name is greater than 30, will be the left more than the character of the cut off, but less than 30, there will be no spaces.4)%20.30c: If the category name is less than 20, fill in the blanks, and right-aligned, if its name is longer than 30 characters, it is exported from the left hand, the characters are cut off.
1
6. Output the log to a text file log4j
Configuration information of Log4j. xml and Log4j. xml
Source code
1
Log4j. xml configuration
Value = "[% d {dd HH: mm: ss, SSS/} %-5 p] [% t] % c {2/}-% m % n"/>Value = "[% d {MMdd HH: mm: ss SSS/} %-5 p] [% t] % c {3/}-% m % n"/>
(1). There are generally five appender output methods:Org. apache. log4j. RollingFi
Log4j is an open source project for Apache, by using log4j, we can control the destination of log information delivery is console, file, GUI component, even interface server, NT Event recorder, UNIX syslog daemon, etc. We can also control the output format of each log, and by defining the level of each log information, we can control the log generation process more carefully.
LOG4J configuration: Instead of using configuration files at all, you can configure the LOG4J environment in your code. However, using a configuration file will make your application more flexible. LOG4J supports two configuration file formats, one in XML format and the properties file (key = value). Log4j has three
The benefits of Log4j 2 will not be discussed with everyone. If you search for 2, it means you have some knowledge about it and want to use it, so you can get started directly here.
1. Download log4j 2 from the official website.To import the jar package, you only need to import the following two jar packages (xx is a messy version ):
Log4j-core-xx.jar
optimization.
The log4j is based on a named logger hierarchy.
LOG4J is a failure stop, but there is no guarantee that each log statement will be delivered to its destination.
Log4j is 线程安全 , the log4j component is designed for a large number of multithreaded systems.
The
] TRACE com.lf.testlog4j.hellolog4j-exiting application.You can control how different Java classes are exported by configuration. For example, I want to set the output level of bar to trace, and the output level of other classes is error, configured as follows:Loggers> Loggername= "Com.lf.testLog4j.Bar" Level= "Trace"additivity= "false"> Appende
Log4j Introduction Basically all large-scale applications, including our commonly used frameworks, such as Hibernate, spring, and struts, provide a certain amount of log information within them. Why is this log information hard-coded in the system? It is a way to debug the system and observe the system running status. Most programmers may remember that when they first wrote code, they always failed to write a method and liked to use system. out. print
name.You can specify a log level for logger instances (such as through the Setlevel interface) and currently support these common levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL. Do not considerTRACE, the remaining levels are important in the following order:DEBUG .If a logger instance does not explicitly specify a level, it inherits the rank of the parent logger instance that is closest to it and is explicitly specified by the log level.Once a level
configuration file under Classpath named Log4j.properties. If you change this profile to a different name, such as My.properties, the program can still run, but it will report a prompt to initialize the log4j system incorrectly. You can then add the following in your program:
Propertyconfigurator.configure ("Classes/my.properties");
Problem can be solved.
4, log4j log4j.properties configuration o
When Tomcat is started, the following warning is displayed:
Log4j: warn no appenders cocould be found for logger (Org. Apache. commons. digester. digester. Sax ).Log4j: Warn please initialize the log4j system properly.
Solution:
The log4j. properties file must be placed under the web-INF/class directory and placed u
In the "Spring" Spring3.0.5 download, Configuration and HelloWorld (click to open the link) of the article you may have seen. If spring does not integrate log4j directly, the following warning about spring consolidation log4j appears. This is very annoying, on the one hand advocating high cohesion, low coupling, on the other hand, they did not integrate log4j on
. Configure com. abc. mapper
# The Log Level of all classes under the package
Log4j.logger.com. abc. mapper = DEBUG
# Use the following 10 and 11 lines to configure the getById of TeacherMapper and TeacherMapper.
# Method log level. Log4j.logger.com. abc. mapper. TeacherMapper
# The prefix of log4j.logger.com. abc. mapper indicates
# Log4j.logger.com. abc. mapper is its parent logger. Others and so on
# Log4j.logger.com. abc. mapper. TeacherMapper =
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.