In the whole web system, in order to unify the use of log management, it is necessary to load the log4j configuration file when the system is started, in order to ensure that the format of the later use of log4j is consistent, easy to track and solve the problem. So, how to load the log4j
The website is about to be released. To track some logs, you need to use log4j. So I studied the configuration of log4j.
First, paste a configuration source file log4j. properties.
log4j.rootLogger=DEBUG, CONSOLE, FILE## for consolelog4j.appender.CONSOLE=org.apache.log4j.Co
In the selection of slf4j-api-1.7.7.jar + slf4j-log4j12-1.7.7.jar + log4j-1.2.17.jar output (the premise is that there is a corresponding configuration file log4j. properties, put in the root directory, usually SRC folder) to the file logs. log and errors. Log
1. The basic format is as follows:
# Configure root loggerlog4j. rootlogger = [level], appendername1, a
The log4j configuration file is used to set the level, the repository, and the layout of the logger, which can be set in key=value format or in XML format.By configuration, you can create a running environment for log4j.1. The basic format of the
}" for jar in $lib _path/*.jardoif ["$CLASSPATH" = ""];Thenclasspath= $jarelseCLASSPATH = $CLASSPATH: $jarfidoneexport classpath= $CLASSPATHecho "java ${main_class} \" [Email Protected]\ ""4. Right-click Project-debug as-maven assembly:assemblyThe output log is packaged successfully.Four. Testing1. Unzip the zip file, the folder structure is as follows, all files are present:2. Enter the bin and run the Start.bat (the JRE environment needs to be installed in the running environment)3. The result
files!");
}
StringRoot = Servletcontext.getrealpath ("/");
if(Root = =
NULL){
Throw
New
illegalstateexception("Cannot set web App root system" when WAR file isn't
Expanded ");
}
System. setProperty (key, Root);
ServletContext.log ("Set Web App root system property:" + key + "=" + root);
}
From the code, this method actually saves the absolute file path of the root directory of the Web application as a property in the system's list of properties. The name of the property, indicated
}web-inf/logs/schedule.loglog4j.appender.e.append=Truelog4j.appender.e.threshold=errorlog4j.appender.e.maxfilesize=50000kblog4j.appender.e.layout=Org.apache.log4j.patternlayoutlog4j.appender.e.layout.conversionpattern=%-d{yyyy-mm-dd Hh\:mm\:ss}[%l\:%c\:%t\:%r]-[%p]%m%nThird, configure the Load Log4j.properties property in the Web. xml fileload the log4j configuration
project.Log4j.logger.com.opensymphony.oscache=errorLog4j.logger.net.sf.navigator=errorThese two sentences are to set the error level below the two packages as errors, assuming that there is no configuration Ehcache in the project, then these two sentences are not required.Log4j.logger.org.apache.commons=errorLog4j.logger.org.apache.struts=warnThese two sentences are struts's packages.Log4j.logger.org.displaytag=errorThis sentence is Displaytag's bag.
The log4j configuration file (Configuration file) is used to set the level, the repository, and the layout of the logger, and it can be set in key=value format or in XML format information. By configuring, you can create a log4j running environment.
1.
# -- Log4j. rootlogger: output the log information with the info level to the stdout and file destinations. The definitions of stdout and file can be named in the following code. Levels can be divided into off, fatal, error, warn, info, debug, and all. If off is configured, no information is displayed. If Info is configured, only info, warn is displayed, error Log information, while debug information is not displayed
configures the LOG4J environment with the following syntax: /** Basicconfigurator.configure (); Automatically and quickly use the default log4j environment. * Propertyconfigurator.configure (* "D:\\project\\eclipse\\log4j\\src\\log4j.properties"): * Read the properties file using Java The configuration file tha
The log4j configuration file is used to set the level, the repository, and the layout of the logger, which can be set in key=value format or in XML format. By configuration, you can create a running environment for log4j.Steps to create log4j:
The associated jar pac
by the QC Problem List page)Log4j.logger.org.springframework=debugThis sentence is spring's package.Log4j.logger.org.hibernate.ps.preparedstatementcache=warnLog4j.logger.org.hibernate=debugThese two sentences are Hibernate's package.The above package settings can be customized according to the actual situation of the project.Three, log4j detailed1. Define the configuration fileLOG4J supports two
= Loader.getresource (default_xml_configuration_file); if (url = = N ull) {url = Loader.getresource (default_configuration_file);} } else {try {url = new URL (configurationoptionstr)} catch (Malformedurlexception ex) {//So, resource are not a URL: Attempt to get the resource from the class path URL = Loader.getresource (CONFIGURATIONOPTIONSTR); }}//If We have a non-null URL and then delegate the rest of the//configuration to the Optionconvert
Grails uses log4j log information and log4j configuration information is written in Config.groovy files.
One, Grails 1.1 can specify different logging levels for the code under different packages, such as:
log4j = { // 设置控制器和GSP页面的级别为error error 'org.codehaus.groovy.grails.web.servlet', // controllers 'org.codehau
Configure log4j log, first import Slf4j-log4j12-1.7.7.jarConfiguring in Maven
When you use the properties file configuration
### set log levels ###
Log4j.rootlogger = debug,d,e LOG4J.APPENDER.D
= Org.apache.log4j.DailyRollingFileAppender
log4j.appender.d.file = ${webapp.root}/logs/common-default.log
Log4j.appender.d.append = True
log4j.appender.d.threshold = INFO
log4j.appender.d.layout = Org.apache.l
When the project is on-line, the runtime often also has an exception, in the exception throws, want to get feedback immediately. So you need to configure the log4j send email function.The original version of log4j used in the project is 1.2.9, but this version does not support the authentication function of the mail service, so the latest version 1.2.14 is downloaded again to view the source codeThis versio
profile path.
Set Value VM System variables:Simply, by setting the value of the VM system variable log4j.configuration to the configuration file path before getting the Logmanager initialization value, you can achieve the purpose:
static {
String Customizedpath = "Log/log4j.xml";
System.setproperty ("Log4j.configuration", Customizedpath);
Logger = Logmanager.getlogger (Log4jonetest.class);
}
propertyconfigurator or Domconfigurator:Domcon
Original address: http://www.hankcs.com/program/java/log4j-2-console-color-configuration-with-intellij-idea.html
If you are ready to open a large project, the log system is essential. LOG4J is the best log component on the Java platform, and the log4j 2 upgrades a lot of APIs and is more scalable.
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.