java - log4j2 + slf4j configuration for JDBC connection -


i'm new log4j , i'm having hard time getting started. have decided use log4j2 in project , started replacing apache shiro's logging implementation jcl log4j2.

i managed work on console, not on jdbc. database connection works nulls inserted. literal values work, not patterns.

pom.xml

<!-- logging api + implementation: -->     <dependency>         <groupid>org.slf4j</groupid>         <artifactid>slf4j-api</artifactid>         <version>1.7.12</version>     </dependency>     <dependency>         <groupid>org.apache.logging.log4j</groupid>         <artifactid>log4j-slf4j-impl</artifactid>         <version>2.3</version>     </dependency>     <dependency>         <groupid>org.apache.logging.log4j</groupid>         <artifactid>log4j-api</artifactid>         <version>2.3</version>     </dependency>     <dependency>         <groupid>org.apache.logging.log4j</groupid>         <artifactid>log4j-core</artifactid>         <version>2.3</version>     </dependency> 

log4j2.xml

<?xml version="1.0" encoding="utf-8"?> <configuration status="trace" monitorinterval="30">     <appenders>         <console name="console" target="system_out">             <patternlayout pattern="%d{hh:mm:ss.sss} [%t] %-5level %logger{36} - %msg%n" />         </console>         <jdbc name="databaseappender" tablename="log">             <connectionfactory class="my.logging.logconnectionfactory"                 method="getdatabaseconnection" />             <!-- <column name="id" literal="logging.application_log_sequence.nextval" /> -->             <column name="dt_cret" iseventtimestamp="true" />             <column name="vl_header" pattern="%c" />             <column name="vl_level" pattern="%l" />             <column name="vl_msg" pattern="%m" />             <column name="nm_user" literal="'cosme'" />             <!-- <column name="nm_user" pattern="%x{username}" />-->         </jdbc>     </appenders>     <loggers>         <root level="info">             <appenderref ref="console" level="info" />             <appenderref ref="databaseappender" level="info" />         </root>     </loggers> </configuration> 

logconnectionfactory.java

package my.logging;  import java.sql.connection; import java.sql.sqlexception;  import javax.sql.datasource;  import org.apache.commons.dbcp2.connectionfactory; import org.apache.commons.dbcp2.drivermanagerconnectionfactory; import org.apache.commons.dbcp2.poolableconnection; import org.apache.commons.dbcp2.poolableconnectionfactory; import org.apache.commons.dbcp2.poolingdatasource; import org.apache.commons.pool2.objectpool; import org.apache.commons.pool2.impl.genericobjectpool;  import my.config.configurationmanager;   public class logconnectionfactory {      private static interface singleton {         final logconnectionfactory instance = new logconnectionfactory();     }      private final datasource datasource;      private logconnectionfactory() {         try {             class.forname(configurationmanager.getvalue("mtdt_db.connectiondriver")).newinstance();         } catch (exception e) {             // todo auto-generated catch block             e.printstacktrace();         }         this.datasource = setupdatasource(configurationmanager.getvalue("mtdt_db.connectionstring"));     }      public static connection getdatabaseconnection() throws sqlexception {         return singleton.instance.datasource.getconnection();     }      public static datasource setupdatasource(string connecturi) {         //         // first, we'll create connectionfactory         // pool use create connections.         // we'll use drivermanagerconnectionfactory,         // using connect string passed in command line         // arguments.         //         connectionfactory connectionfactory = new drivermanagerconnectionfactory(connecturi,null);          //         // next we'll create poolableconnectionfactory, wraps         // "real" connections created connectionfactory         // classes implement pooling functionality.         //         poolableconnectionfactory poolableconnectionfactory = new poolableconnectionfactory(connectionfactory, null);          //         // we'll need objectpool serves         // actual pool of connections.         //         // we'll use genericobjectpool instance, although         // objectpool implementation suffice.         //         objectpool<poolableconnection> connectionpool = new genericobjectpool<>(poolableconnectionfactory);          // set factory's pool property owning pool         poolableconnectionfactory.setpool(connectionpool);          //         // finally, create poolingdriver itself,         // passing in object pool created.         //         poolingdatasource<poolableconnection> datasource = new poolingdatasource<>(connectionpool);          return datasource;     } } 

database output:

database output

console output (seems correct):

console output

i got work using isunicode="false" on columns...

log4j2.xml

<?xml version="1.0" encoding="utf-8"?> <configuration status="trace" monitorinterval="30">     <appenders>         <console name="console" target="system_out">             <patternlayout pattern="%d{hh:mm:ss.sss} [%t] %-5level %logger{36} - %msg%n" />         </console>         <jdbc name="databaseappender" tablename="log">             <connectionfactory class="pt.mapidea.logging.logconnectionfactory"                 method="getdatabaseconnection" />             <!-- <column name="id" literal="logging.application_log_sequence.nextval" /> -->             <column name="dt_cret" iseventtimestamp="true" />             <column name="vl_header" pattern="%c" isunicode="false" />             <column name="vl_level" pattern="%l" isunicode="false" />             <column name="vl_msg" pattern="%m" isunicode="false" />             <column name="nm_user" literal="'cosme'" />             <!-- <column name="nm_user" pattern="%x{username}" />-->         </jdbc>     </appenders>     <loggers>         <root level="info">             <appenderref ref="console" level="info" />             <appenderref ref="databaseappender" level="info" />         </root>     </loggers> </configuration> 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -