Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component
- public class Log4g2DbConfig {
- private final Level LOGGER_LEVEL = Level.ERROR;
- private static Logger LOGGER = LogManager.getLogger(Log4g2DbConfig.class);
- @Autowired
- private HikariDataSource dataSource;
- @Autowired
- private AppConfig appConfig;
- @Autowired
- private EntityManager em;
- //inner class
- class Log4jConnect implements ConnectionSource {
- private DataSource dsource;
- public Log4jConnect(DataSource dsource) {
- this.dsource = dsource;
- }
- @Override
- public Connection getConnection() throws SQLException {
- return dsource.getConnection();
- }
- }
- @PostConstruct
- private void init(){
- System.out.println("init LogUtils");
- try {
- // set hostName into System's property, which will use by log4j
- System.setProperty("hostName_custom", (InetAddress.getLocalHost().getHostAddress() + " : " + InetAddress.getLocalHost().getHostName()));
- } catch (Exception e) {
- LOGGER.error("set hostName into System's property",e);
- }
- final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
- final Configuration config = ctx.getConfiguration();
- //map Database Columns&&Patterns
- ColumnConfig[] cc = {
- ColumnConfig.createColumnConfig(config, "L_DATE", null, null, "true", null, null),
- ColumnConfig.createColumnConfig(config, "L_CLASS", "%logger", null, null, null, null),
- /* ColumnConfig.createColumnConfig(config, "L_LEVEL", "%level", null, null, null, null),
- ColumnConfig.createColumnConfig(config, "L_MESSAGE", "%message", null, null, null, null),
- ColumnConfig.createColumnConfig(config, "L_HOST", "${sys:hostName_custom}", null, null, null, null),
- ColumnConfig.createColumnConfig(config, "L_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 Log4jConnect(dataSource), "0", "SHEMANAME.TEMP_LOG", cc);
- appender.start();
- config.addAppender(appender);
- LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
- //Logger LEVEL
- loggerConfig.addAppender(appender, LOGGER_LEVEL, null);
- ctx.updateLoggers();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement