java - How to set the DB Credentials of log4j at runtime -


this log4j properties file. i'm writing logs db. want set db credentials @ run time

log4j.appender.db=org.apache.log4j.jdbc.jdbcappender log4j.appender.db.url=jdbc:sqlserver://172.16.0.201:1433;databasename=databaseone;autoreconnect=true log4j.appender.db.driver=com.microsoft.sqlserver.jdbc.sqlserverdriver log4j.appender.db.user=username log4j.appender.db.password=password$123 log4j.appender.db.sql=insert usage_fact(accessed_date,accessed_item_id,user_id,tenant_id,log_level) values('%d{yyyy-mm-dd hh:mm:ss}','%x{accessed_item_id}','%x{user_id}','%x{tenant_id}','%p') log4j.appender.db.layout=org.apache.log4j.patternlayout 

and how catch sql exception arises while log4j connecting db or writing table. exception stack trace printing on console don't want print on console.

you can initialize logging framework through java code this:

public class corelogger {     public static logger getlogger(final class modulename) {         logger logger = logger.getlogger(modulename);         properties prop = new properties();         try {             prop.load(new filereader("log4j.properties"));         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }         prop.put("log4j.appender.db.user", "root");         prop.put("log4j.appender.db.password", "root");         propertyconfigurator.configure(prop);         return logger;     } } 

and use

static logger log = corelogger.getlogger(demo.class); 

in classes instance of logger, way can set credentials @ run time.

regarding capturing errors in files, can redirect stdout , stderr log files refer dario's answer @ log4j redirect stdout dailyrollingfileappender more details that.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -