Search in sources :

Example 1 with LogbackConfig

use of com.adeptj.runtime.tools.logging.LogbackConfig in project adeptj-modules by AdeptJ.

the class LoggingConfigFactory method start.

@Activate
protected void start(LoggingConfig config) {
    String level = config.level();
    String logFile = config.logFile();
    String[] loggerNames = config.loggerNames();
    boolean additivity = config.additivity();
    LogbackManager logbackMgr = LogbackManager.INSTANCE;
    // If configuration is for error.log, add the configured loggers to both FILE and CONSOLE appender.
    if (StringUtils.endsWith(logFile, DEFAULT_LOG_FILE)) {
        logbackMgr.addLogger(LogbackConfig.builder().loggers(loggerNames).level(level).additivity(additivity).appenders(logbackMgr.getAppenders()).build());
        return;
    }
    // If no log file provided then add the configured loggers only to CONSOLE appender.
    if (StringUtils.endsWith(logFile, SLASH)) {
        logbackMgr.addLogger(LogbackConfig.builder().loggers(loggerNames).level(level).additivity(additivity).appender(logbackMgr.getAppender(APPENDER_CONSOLE)).build());
        return;
    }
    String rolloverFile = config.rolloverFile();
    String pattern = config.pattern();
    int logMaxHistory = config.logMaxHistory();
    String logMaxSize = config.logMaxSize();
    boolean immediateFlush = config.immediateFlush();
    boolean addAsyncAppender = config.addAsyncAppender();
    int asyncLogQueueSize = config.asyncLogQueueSize();
    int asyncLogDiscardingThreshold = config.asyncLogDiscardingThreshold();
    LogbackConfig logbackConfig = LogbackConfig.builder().appenderName(UUID.randomUUID().toString()).asyncAppenderName(UUID.randomUUID().toString()).level(level).logFile(logFile).additivity(additivity).rolloverFile(rolloverFile).pattern(pattern).logMaxHistory(logMaxHistory).logMaxSize(logMaxSize).immediateFlush(immediateFlush).addAsyncAppender(addAsyncAppender).asyncLogQueueSize(asyncLogQueueSize).asyncLogDiscardingThreshold(asyncLogDiscardingThreshold).loggers(loggerNames).build();
    RollingFileAppender<ILoggingEvent> fileAppender = logbackMgr.createRollingFileAppender(logbackConfig);
    logbackConfig.getAppenders().add(fileAppender);
    if (logbackConfig.isAddAsyncAppender()) {
        logbackConfig.setAsyncAppender(fileAppender);
        logbackMgr.addAppender(fileAppender).createAsyncAppender(logbackConfig);
    }
    logbackMgr.addLogger(logbackConfig);
}
Also used : LogbackConfig(com.adeptj.runtime.tools.logging.LogbackConfig) LogbackManager(com.adeptj.runtime.tools.logging.LogbackManager) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) Activate(org.osgi.service.component.annotations.Activate)

Example 2 with LogbackConfig

use of com.adeptj.runtime.tools.logging.LogbackConfig in project adeptj-runtime by AdeptJ.

the class LogbackInitializer method init.

public static void init() {
    long startTime = System.nanoTime();
    Config loggingCfg = Configs.DEFAULT.logging();
    LogbackConfig logbackConfig = getLogbackConfig(loggingCfg);
    LogbackManager logbackMgr = LogbackManager.INSTANCE;
    RollingFileAppender<ILoggingEvent> fileAppender = logbackMgr.createRollingFileAppender(logbackConfig);
    ConsoleAppender<ILoggingEvent> consoleAppender = logbackMgr.createConsoleAppender(APPENDER_CONSOLE, loggingCfg.getString(KEY_LOG_PATTERN_CONSOLE));
    List<Appender<ILoggingEvent>> appenderList = new ArrayList<>();
    appenderList.add(consoleAppender);
    appenderList.add(fileAppender);
    logbackMgr.getAppenders().addAll(appenderList);
    LoggerContext context = logbackMgr.getLoggerContext();
    initRootLogger(context, consoleAppender, loggingCfg);
    addLoggers(loggingCfg, appenderList);
    addAsyncAppender(loggingCfg, fileAppender);
    context.start();
    context.getLogger(LogbackInitializer.class).info(INIT_MSG, elapsedMillis(startTime));
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Appender(ch.qos.logback.core.Appender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) LogbackConfig(com.adeptj.runtime.tools.logging.LogbackConfig) LogbackManager(com.adeptj.runtime.tools.logging.LogbackManager) Config(com.typesafe.config.Config) LogbackConfig(com.adeptj.runtime.tools.logging.LogbackConfig) ArrayList(java.util.ArrayList) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext)

Aggregations

ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)2 LogbackConfig (com.adeptj.runtime.tools.logging.LogbackConfig)2 LogbackManager (com.adeptj.runtime.tools.logging.LogbackManager)2 LoggerContext (ch.qos.logback.classic.LoggerContext)1 Appender (ch.qos.logback.core.Appender)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)1 Config (com.typesafe.config.Config)1 ArrayList (java.util.ArrayList)1 Activate (org.osgi.service.component.annotations.Activate)1