Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1.  
  2. @Component
  3. public class Log4g2DbConfig {
  4.  
  5.     private final Level LOGGER_LEVEL = Level.ERROR;
  6.  
  7.     private static Logger LOGGER = LogManager.getLogger(Log4g2DbConfig.class);
  8.  
  9.  
  10.  @Autowired
  11.  private HikariDataSource dataSource;
  12.  
  13.  
  14.     @Autowired
  15.     private  AppConfig appConfig;
  16.  
  17.     @Autowired
  18.     private  EntityManager em;
  19.  
  20.  
  21.  
  22.     //inner class
  23.     class Log4jConnect implements ConnectionSource {
  24.         private DataSource dsource;
  25.  
  26.         public Log4jConnect(DataSource dsource) {
  27.             this.dsource = dsource;
  28.         }
  29.  
  30.         @Override
  31.         public Connection getConnection() throws SQLException {
  32.             return dsource.getConnection();
  33.         }
  34.     }
  35.  
  36.  
  37.  
  38.     @PostConstruct
  39.     private void init(){
  40.         System.out.println("init LogUtils");
  41.         try {
  42.             // set hostName into System's property, which will use by log4j
  43.             System.setProperty("hostName_custom", (InetAddress.getLocalHost().getHostAddress() + " : " + InetAddress.getLocalHost().getHostName()));
  44.         } catch (Exception e) {
  45.            LOGGER.error("set hostName into System's property",e);
  46.         }
  47.  
  48.         final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
  49.         final Configuration config = ctx.getConfiguration();
  50.         //map Database Columns&&Patterns
  51.         ColumnConfig[] cc = {
  52.                 ColumnConfig.createColumnConfig(config, "L_DATE", null, null, "true", null, null),
  53.                 ColumnConfig.createColumnConfig(config, "L_CLASS", "%logger", null, null, null, null),
  54.                  /* ColumnConfig.createColumnConfig(config, "L_LEVEL", "%level", null, null, null, null),
  55.  
  56.                 ColumnConfig.createColumnConfig(config, "L_MESSAGE", "%message", null, null, null, null),
  57.                 ColumnConfig.createColumnConfig(config, "L_HOST", "${sys:hostName_custom}", null, null, null, null),
  58.                  ColumnConfig.createColumnConfig(config, "L_THROWABLE", "%ex{short}", null, null, null, null)*/
  59.                 //  ColumnConfig.createColumnConfig(config, "salarie_id", "%X{SALARIE_ID}", null, null, null, null)
  60.         } ;
  61.         Appender appender = JdbcAppender.createAppender("databaseAppender", "true", null, new Log4jConnect(dataSource), "0", "SHEMANAME.TEMP_LOG", cc);
  62.         appender.start();
  63.         config.addAppender(appender);
  64.         LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
  65.         //Logger LEVEL
  66.         loggerConfig.addAppender(appender, LOGGER_LEVEL, null);
  67.         ctx.updateLoggers();
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement