i log log4j2 messages relational database.
documentation jdbc appender here. can use database connection provider these sources:
- connectionfactory
- datasource
- drivermanager
but there way how use datasource bean (com.jolbox.bonecp.bonecpdatasource) use in entire application?
you can in 4 steps (step 2 & 3 log4j2 config web app):
step 1: create log4j2.xml file (without jdbc appender) , put in web-inf folder
<?xml version="1.0" encoding="utf-8"?> <configuration status="info"> <appenders> <console name="console" target="system_out"> <patternlayout pattern="%d{hh:mm:ss.sss} [%t] %-5level %logger{36} - %msg%n"/> </console> </appenders> <loggers> <root level="info"> <appenderref ref="console"/> </root> </loggers> </configuration>
step 2 : add log4j2-web-2.x.jar web-inf/lib.
here maven repository, can download jar there or copy paste dependency pom.xml if using maven.
step 3 : configure web.xml depending on servlet api version
see log4j2 documentation more details. here how configure log4j in servlet 2.5 web app.
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5"> <!-- logg --> <listener> <listener-class>org.apache.logging.log4j.web.log4jservletcontextlistener</listener-class> </listener> <filter> <filter-name>log4jservletfilter</filter-name> <filter-class>org.apache.logging.log4j.web.log4jservletfilter</filter-class> </filter> <filter-mapping> <filter-name>log4jservletfilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>request</dispatcher> <dispatcher>forward</dispatcher> <dispatcher>include</dispatcher> <dispatcher>error</dispatcher> </filter-mapping> <!-- /logg --> <!-- ... --> </web-app>
step 4: create spring bean , inject datasource bean , , add jdbc appender configuration dynamically in @postconstruct method
here code sample ( tested log4j 2.1 , spring 3.5)
import java.sql.connection; import java.sql.sqlexception; import javax.annotation.postconstruct; import javax.sql.datasource; import org.apache.logging.log4j.logmanager; import org.apache.logging.log4j.core.appender; import org.apache.logging.log4j.core.loggercontext; import org.apache.logging.log4j.core.appender.db.jdbc.columnconfig; import org.apache.logging.log4j.core.appender.db.jdbc.connectionsource; import org.apache.logging.log4j.core.appender.db.jdbc.jdbcappender; import org.apache.logging.log4j.core.config.appenderref; import org.apache.logging.log4j.core.config.configuration; import org.apache.logging.log4j.core.config.loggerconfig; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.component; @component public class logutils{ @autowired private datasource datasource; //inner class class connect implements connectionsource { private datasource dsource; public connect(datasource dsource) { this.dsource = dsource; } @override public connection getconnection() throws sqlexception { return this.dsource.getconnection(); } } public logutils() { system.out.println("logutils"); } @postconstruct private void init(){ system.out.println("init logutils"); final loggercontext ctx = (loggercontext) logmanager.getcontext(false); final configuration config = ctx.getconfiguration(); columnconfig[] cc = { columnconfig.createcolumnconfig(config, "date", null, null, "true", null, null), columnconfig.createcolumnconfig(config, "level", "%level", null, null, null, null), columnconfig.createcolumnconfig(config, "logger", "%logger", null, null, null, null), columnconfig.createcolumnconfig(config, "message", "%message", null, null, null, null), columnconfig.createcolumnconfig(config, "throwable", "%ex{short}", null, null, null, null), columnconfig.createcolumnconfig(config, "salarie_id", "%x{salarie_id}", null, null, null, null) } ; appender appender = jdbcappender.createappender("databaseappender", "true", null, new connect(datasource), "0", "sicdb.bo_log", cc); appender.start(); config.addappender(appender); loggerconfig loggerconfig = config.getloggerconfig(logmanager.root_logger_name); loggerconfig.addappender(appender, null, null); ctx.updateloggers(); } public datasource getdatasource() { return datasource; } public void setdatasource(datasource datasource) { this.datasource = datasource; } }
** limitations : link doc**
applications have need customize logging separate actual configuration. log4j allows although suffers few limitations: 1. if configuration file changed configuration reloaded , manual changes lost. 2. modification running configuration requires methods being called (addappender , addlogger) synchronized. such, recommended approach customizing configuration extend 1 of standard configuration classes, override setup method first super.setup() , add custom appenders, filters , loggerconfigs configuration before registered use.
Comments
Post a Comment