For a product or project, a detailed log is usually printed during testing. After the product is released, the highest level of log4j is usually set to Improve the efficiency, once a problem occurs to the customer, it is important to check the detailed log information to confirm the log printing. However, it is not good to let the customer manually modify the log4j configuration file.
Modify the log4j level
Public class log4jconfig {
Public void enableinfo (string target ){
Logmanager. getlogger (target). setlevel (level. info );
}
Public void enablewarn (string target ){
Logmanager. getlogger (target). setlevel (level. Warn );
}
Public void enableerror (string target ){
Logmanager. getlogger (target). setlevel (level. Error );
}
Public void enabledebug (string target ){
Logmanager. getlogger (target). setlevel (level. Debug );
}
}
Writing a JSP can be simple and jstl can be used properly.
<% @ Page import = "org. Apache. log4j. *" %>
<% @ Taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %>
<% @ Page contenttype = "text/html; charset = GBK" Language = "Java" %>
<HTML>
<Head> <title> log4j Level Control </title> <Meta http-equiv = "Content-Type" content = "text/html; charset = GBK"/>
<Body>
<H1> log4j Level Control <% String LOGNAME = request. getparameter ("log ");
If (null! = LOGNAME ){
Logger log = ("". Equals (LOGNAME )?
Logger. getrootlogger (): logger. getlogger (LOGNAME ));
Log. setlevel (level. tolevel (request. getparameter ("level"), level. Debug ));
}
%>
<C: Set Var = "rootlogger" value = "<% = logger. getrootlogger () %>"/>
<Form>
<Table border = "1">
<Tr>
<TH> level </Th>
<TH> logger </Th>
<TH> set new level </Th>
</Tr>
<Tr>
<TD >$ {rootlogger. Level} </TD>
<TD >$ {rootlogger. name} </TD>
<TD>
<C: fortokens Var = "level" delims = "," items = "debug, info, warn, error, off">
<A href = "log4j. jsp? Log = & level =$ {level} ">$ {level} </a>
</C: fortokens>
</TD>
</Tr>
<C: foreach Var = "logger" items = "$ {rootlogger. loggerrepository. currentloggers}">
<C: If test = "$ {! Empty logger. level. syslogequivalent | Param. showall} ">
<Tr>
<TD >$ {logger. Level} </TD>
<TD >$ {logger. name} </TD>
<TD>
<C: fortokens Var = "level" delims = "," items = "debug, info, warn, error, off">
<A href = "log4j. jsp? Log =$ {logger. name} & level =$ {level} ">$ {level} </a>
</C: fortokens>
</TD>
</Tr>
</C: If>
</C: foreach>
<Tr>
<TD> </TD>
<TD> <input type = "text" name = "log"/> </TD>
<TD>
<Select name = "level">
<C: fortokens Var = "level" delims = "," items = "debug, info, warn, error, off">
<Option >$ {level} </option>
</C: fortokens>
</SELECT> <input type = "Submit" value = "Add new logger"/> </TD>
</Tr>
</Table>
</Form>
Show <a href = "log4j. jsp? Showall = true "> all known loggers </a>
</Body>
</Html>
If spring 2.0 and JMX are used, the [servletname]-servlet. xml configuration will be simple.
<Bean id = "exporter"
Class = "org. springframework. JMX. Export. mbeanexporter">
<Property name = "beans">
<Map>
<Entry key = "Bean: Name = logbean"
Value-ref = "/hello.htm"/>
</Map>
</Property>
</Bean>
Add two other configurations to spring
<Entry key = "Bean: Name = logbean" value-ref = "logmanager"/>
<Bean name = "logmanager" class = "com. XX. log4jmanager"/>
Write
Public class log4jmanager {
Public void setlogger (string packagename, string level ){
Logmanager. getlogger (packagename). setlevel (level. tolevel (level ));
}
}
You can use jconsole for control.
Typical log4j configuration file
# For JBoss: Avoid to setup log4j outside $ jboss_home/Server/default/deploy/log4j. xml!
# For all other servers: comment out the log4j listener in Web. XML to activate log4j.
Log4j. rootlogger = info, stdout, logfile
Log4j. appender. stdout = org. Apache. log4j. leleappender
Log4j. appender. stdout. layout = org. Apache. log4j. patternlayout
Log4j. appender. stdout. layout. conversionpattern = % d % P [% C]-<% m> % N
Log4j. appender. logfile = org. Apache. log4j. rollingfileappender
Log4j. appender. logfile. File =.../mgrweb/log/mgrweb. Log
Log4j. appender. logfile. maxfilesize = 1024kb
# Keep three backup files.
Log4j. appender. logfile. maxbackupindex = 20
# Pattern to output: date priority [category]-message
Log4j. appender. logfile. layout = org. Apache. log4j. patternlayout
Log4j. appender. logfile. layout. conversionpattern = % d % P [% C \: % t \: % L]-% m % N
Log4j.logger.org. hibernate. SQL = debug