ch.qos.logback.classic.encoder.PatternLayoutEncoder Java Examples

The following examples show how to use ch.qos.logback.classic.encoder.PatternLayoutEncoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DiagnosticLogger.java    From synopsys-detect with Apache License 2.0 7 votes vote down vote up
private FileAppender<ILoggingEvent> addAppender(final String file) {
    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final PatternLayoutEncoder ple = new PatternLayoutEncoder();

    ple.setPattern("%date %level [%file:%line] %msg%n");
    ple.setContext(lc);
    ple.start();

    final FileAppender<ILoggingEvent> appender;
    appender = new FileAppender<>();
    appender.setFile(file);
    appender.setEncoder(ple);
    appender.setContext(lc);
    final ThresholdFilter levelFilter = new ThresholdFilter();
    levelFilter.setLevel(this.level.levelStr);
    levelFilter.start();
    appender.addFilter(levelFilter);

    appender.start();

    final ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    logbackLogger.addAppender(appender);

    return appender;
}
 
Example #2
Source File: MaskingConverterTest.java    From owasp-security-logging with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
	PatternLayout.defaultConverterMap.put("mask",
			MaskingConverter.class.getName());

	encoder = new PatternLayoutEncoder();
	encoder.setContext(loggerContext);
	encoder.setPattern("%-4relative [%thread] %-5level %logger{35} - %mask%n");
	encoder.start();

	mockAppender.setContext(loggerContext);
	mockAppender.setEncoder(encoder);
	mockAppender.start();

	LOGGER.addAppender(mockAppender);
}
 
Example #3
Source File: LoggingPersistenceService.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
protected FileAppender<ILoggingEvent> createNewAppender(String alias) {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(context);
    encoder.setPattern(pattern);
    encoder.start();

    FileAppender<ILoggingEvent> appender = new FileAppender<ILoggingEvent>();
    appender.setAppend(true);
    appender.setFile(LOG_FOLDER + File.separator + alias + LOG_FILEEXT);
    appender.setEncoder(encoder);
    appender.setContext(context);
    appender.start();

    return appender;
}
 
Example #4
Source File: LogbackLogConfigurator.java    From butterfly with MIT License 6 votes vote down vote up
@Override
    public void setVerboseMode(boolean verboseMode) {
        this.verboseMode = verboseMode;

        if (verboseMode) {
            PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
            patternLayoutEncoder.setPattern("[%d{HH:mm:ss.SSS}] [%highlight(%level)] %msg%n");
            patternLayoutEncoder.setContext(loggerContext);
            patternLayoutEncoder.start();

            Appender<ILoggingEvent> consoleAppender = new ConsoleAppender();
            ((OutputStreamAppender) consoleAppender).setEncoder(patternLayoutEncoder);
            consoleAppender.setContext(loggerContext);
            consoleAppender.start();

            loggerContext.getLogger("com.paypal.butterfly.cli").detachAppender("CONSOLE");
            loggerContext.getLogger("ROOT").addAppender(consoleAppender);
//        } else {
            // TODO
        }
    }
 
Example #5
Source File: LogbackConfig.java    From g4proxy with Apache License 2.0 6 votes vote down vote up
private static void configLog() {
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.reset();

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setPattern("g4Proxy-%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
    encoder.setContext(context);
    encoder.start();


    LogcatAppender logcatAppender = new LogcatAppender();
    logcatAppender.setContext(context);
    logcatAppender.setEncoder(encoder);
    logcatAppender.setName("logcat1");
    logcatAppender.start();

    Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    root.setLevel(Level.INFO);
    root.addAppender(logcatAppender);

}
 
Example #6
Source File: Emissary.java    From emissary with Apache License 2.0 6 votes vote down vote up
static void setupLogbackForConsole() {
    // So it looks better when commands are run
    ch.qos.logback.classic.Logger root =
            (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
    root.detachAndStopAllAppenders();
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    lc.reset();

    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setPattern("%msg%n");
    ple.setContext(lc);
    ple.start();

    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setEncoder(ple);
    consoleAppender.setContext(lc);
    consoleAppender.start();

    root.addAppender(consoleAppender);
    root.setLevel(Level.INFO);
    root.setAdditive(false);
}
 
Example #7
Source File: LoggerMenu.java    From Hive2Hive with MIT License 6 votes vote down vote up
private static String createRootLogger() {
	LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
	PatternLayoutEncoder ple = new PatternLayoutEncoder();

	ple.setPattern("%d{HH:mm:ss} %-12.-12([%thread])[%-5level] %logger{0} -%msg%n");
	ple.setContext(loggerContext);
	ple.start();

	String fileName = String.format("logs/h2h-log %s.txt",
			new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(System.currentTimeMillis()));
	FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
	fileAppender.setFile(fileName);
	fileAppender.setEncoder(ple);
	fileAppender.setContext(loggerContext);
	fileAppender.start();

	Logger logbackLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
	logbackLogger.addAppender(fileAppender);
	logbackLogger.setLevel(Level.DEBUG);
	logbackLogger.setAdditive(false);

	return fileName;
}
 
Example #8
Source File: BrokerConsoleLoggerImpl.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Override
protected Appender<ILoggingEvent> createAppenderInstance(Context context)
{
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();

    final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setPattern(getLayout());
    encoder.setContext(context);
    encoder.start();

    if (_consoleStreamTarget == ConsoleStreamTarget.STDERR)
    {
        consoleAppender.setTarget("System.err");
    }
    consoleAppender.setEncoder(encoder);

    return consoleAppender;
}
 
Example #9
Source File: StartupAppender.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
public void logToConsole()
{
    Context context = getContext();
    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setContext(context);
    PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
    patternLayoutEncoder.setContext(context);

    // added MDC variable 'qpid.log.prefix' for test purposes
    patternLayoutEncoder.setPattern("%X{qpid.log.prefix}%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
    patternLayoutEncoder.start();

    consoleAppender.addFilter(new Filter<ILoggingEvent>()
    {
        @Override
        public FilterReply decide(final ILoggingEvent event)
        {
            return event.getLevel().isGreaterOrEqual(_consoleAppenderAcceptLogLevel) ? FilterReply.ACCEPT : FilterReply.DENY;
        }
    });

    consoleAppender.setEncoder(patternLayoutEncoder);
    consoleAppender.start();
    replayAccumulatedEvents(consoleAppender);
    consoleAppender.stop();
}
 
Example #10
Source File: DiagnosticLogger.java    From hub-detect with Apache License 2.0 6 votes vote down vote up
private FileAppender<ILoggingEvent> addAppender(final String file) {
    final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    final PatternLayoutEncoder ple = new PatternLayoutEncoder();

    ple.setPattern("%date %level [%file:%line] %msg%n");
    ple.setContext(lc);
    ple.start();
    FileAppender<ILoggingEvent> appender;
    appender = new FileAppender<>();
    appender.setFile(file);
    appender.setEncoder(ple);
    appender.setContext(lc);
    appender.start();

    final ch.qos.logback.classic.Logger logbackLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(LOGBACK_LOGGER_NAME);
    logbackLogger.addAppender(appender);
    logbackLogger.setLevel(Level.ALL);

    return appender;
}
 
Example #11
Source File: LogUtil.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Start a specific file logging, typically for the processing of a given book.
 *
 * @param name      appender name (typically the book radix)
 * @param logFolder target folder where the log file is to be written
 */
public static void addAppender (String name,
                                Path logFolder)
{
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(
            Logger.ROOT_LOGGER_NAME);
    FileAppender fileAppender = new FileAppender();
    PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
    fileAppender.setName(name);
    fileAppender.setContext(loggerContext);
    fileAppender.setAppend(false);

    String now = new SimpleDateFormat("yyyyMMdd'T'HHmm").format(new Date());
    Path logFile = logFolder.resolve(name + "-" + now + ".log");
    fileAppender.setFile(logFile.toAbsolutePath().toString());
    fileEncoder.setContext(loggerContext);
    fileEncoder.setPattern("%date %level [%X{BOOK}%X{SHEET}] %25file:%-4line | %msg%n%ex");
    fileEncoder.start();
    fileAppender.setEncoder(fileEncoder);
    fileAppender.start();
    root.addAppender(fileAppender);
}
 
Example #12
Source File: LogUtil.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Add a specific appender meant for FILE.
 */
public static void addFileAppender ()
{
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(
            Logger.ROOT_LOGGER_NAME);
    FileAppender fileAppender = new FileAppender();
    PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
    fileAppender.setName("FILE");
    fileAppender.setContext(loggerContext);
    fileAppender.setAppend(false);

    String now = new SimpleDateFormat("yyyyMMdd'T'HHmmss").format(new Date());
    Path logPath = WellKnowns.LOG_FOLDER.resolve(now + ".log").toAbsolutePath();
    fileAppender.setFile(logPath.toString());
    fileEncoder.setContext(loggerContext);
    fileEncoder.setPattern(
            "%date %-5level [%X{BOOK}%X{SHEET}] %25replace(%file){'\\.java$',''} %-4line | %msg%n%ex");
    fileEncoder.start();
    fileAppender.setEncoder(fileEncoder);
    fileAppender.start();
    root.addAppender(fileAppender);

    initMessage("LogUtil. Logging to " + logPath);
}
 
Example #13
Source File: Utils.java    From jlineup with Apache License 2.0 6 votes vote down vote up
public static void logToFile(String workingDir) {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    PatternLayoutEncoder ple = new PatternLayoutEncoder();

    ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
    ple.setContext(lc);
    ple.start();
    FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
    fileAppender.setName(JLINEUP_FILE_APPENDER);
    fileAppender.setFile(workingDir + "/jlineup.log");
    fileAppender.setEncoder(ple);
    fileAppender.setContext(lc);
    fileAppender.start();

    ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
    logger.addAppender(fileAppender);
    logger.setLevel(Level.DEBUG);
}
 
Example #14
Source File: KonkerLoggerBasicConfigurator.java    From konker-platform with Apache License 2.0 6 votes vote down vote up
public static void configure(KonkerLoggerContext lc) {
    StatusManager sm = lc.getStatusManager();
    if(sm != null) {
        sm.add(new InfoStatus("Setting up default configuration.", lc));
    }

    ConsoleAppender ca = new ConsoleAppender();
    ca.setContext(lc);
    ca.setName("console");
    PatternLayoutEncoder pl = new PatternLayoutEncoder();
    pl.setContext(lc);
    pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
    pl.start();
    ca.setEncoder(pl);
    ca.start();
    KonkerLogger rootLogger = lc.getLogger("ROOT");
    rootLogger.addAppender(ca);
}
 
Example #15
Source File: LoggingConfig.java    From mirror with Apache License 2.0 6 votes vote down vote up
public synchronized static void enableLogFile() {
  init();

  LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

  PatternLayoutEncoder encoder = new PatternLayoutEncoder();
  encoder.setContext(context);
  encoder.setPattern(pattern);
  encoder.start();

  FileAppender<ILoggingEvent> file = new FileAppender<>();
  file.setContext(context);
  file.setAppend(true);
  file.setFile("mirror.log");
  file.setEncoder(encoder);
  file.start();
  getRootLogger().addAppender(file);
}
 
Example #16
Source File: SelfTestRunner.java    From pravega with Apache License 2.0 6 votes vote down vote up
private static void setupLogging(TestConfig testConfig) {
    val logFile = new java.io.File(testConfig.getTestLogPath());
    if (logFile.delete()) {
        TestLogger.log("Main", "Deleted log file %s.", logFile.getAbsolutePath());
    }

    // Configure slf4j to not log anything (console or whatever). This interferes with the console interaction.
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.getLoggerList().get(0).detachAndStopAllAppenders();

    val fa = new FileAppender<ILoggingEvent>();
    fa.setContext(context);
    fa.setName("selftest");
    fa.setFile(logFile.getAbsolutePath());

    val encoder = new PatternLayoutEncoder();
    encoder.setContext(context);
    encoder.setPattern("%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %level - %msg%n");
    encoder.start();
    fa.setEncoder(encoder);
    fa.start();

    context.getLoggerList().get(0).addAppender(fa);
    context.getLoggerList().get(0).setLevel(Level.INFO);
    //context.reset();
}
 
Example #17
Source File: BaleenLoggerBuilderTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileWithDailyRolling() {
  BaleenFileLoggerBuilder builder =
      new BaleenFileLoggerBuilder(
          NAME,
          BaleenLogging.DEFAULT_PATTERN,
          LOG_FILENAME,
          new MinMaxFilter(Level.INFO, Level.WARN),
          true,
          Optional.of(5),
          Optional.of(10));

  LoggerContext context = new LoggerContext();
  Encoder<ILoggingEvent> encoder = new PatternLayoutEncoder();

  Appender<ILoggingEvent> appender = builder.build(context, encoder);

  assertTrue(appender instanceof FileAppender);
  assertEquals(encoder, ((FileAppender<ILoggingEvent>) appender).getEncoder());

  // TODO: Add tests on the (current private) methods
}
 
Example #18
Source File: BaleenLoggerBuilderTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileWithDailyRollingWithoutSize() {
  BaleenFileLoggerBuilder builder =
      new BaleenFileLoggerBuilder(
          NAME,
          BaleenLogging.DEFAULT_PATTERN,
          LOG_FILENAME,
          new MinMaxFilter(Level.INFO, Level.WARN),
          true,
          Optional.empty(),
          Optional.of(10));

  LoggerContext context = new LoggerContext();
  Encoder<ILoggingEvent> encoder = new PatternLayoutEncoder();

  Appender<ILoggingEvent> appender = builder.build(context, encoder);

  assertTrue(appender instanceof FileAppender);
  assertEquals(encoder, ((FileAppender<ILoggingEvent>) appender).getEncoder());

  // TODO: Add tests on the (current private) methods
}
 
Example #19
Source File: BaleenLoggerBuilderTest.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Test
public void testFileWithoutDailyRolling() {
  BaleenFileLoggerBuilder builder =
      new BaleenFileLoggerBuilder(
          NAME,
          BaleenLogging.DEFAULT_PATTERN,
          LOG_FILENAME,
          new MinMaxFilter(Level.INFO, Level.WARN),
          false,
          Optional.of(5),
          Optional.of(10));

  LoggerContext context = new LoggerContext();
  Encoder<ILoggingEvent> encoder = new PatternLayoutEncoder();

  Appender<ILoggingEvent> appender = builder.build(context, encoder);

  assertTrue(appender instanceof FileAppender);
  assertEquals(encoder, ((FileAppender<ILoggingEvent>) appender).getEncoder());

  // TODO: Add tests on the (current private) methods
}
 
Example #20
Source File: LocalMainCommand.java    From terracotta-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
  Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME);

  if (verbose) {
    ConsoleAppender<ILoggingEvent> appender = (ConsoleAppender<ILoggingEvent>) rootLogger.getAppender("STDOUT");
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %c{1}:%L - %msg%n");
    ple.setContext(appender.getContext());
    ple.start();

    appender.setEncoder(ple);
    rootLogger.setLevel(Level.TRACE);
    rootLogger.getLoggerContext().getLoggerList().forEach(logger -> logger.setLevel(Level.TRACE));
  }
}
 
Example #21
Source File: DynamicLogbackLogger.java    From tddl5 with Apache License 2.0 6 votes vote down vote up
protected Appender buildAppender(String name, String fileName, String pattern) {
    RollingFileAppender appender = new RollingFileAppender();
    appender.setContext(loggerContext);
    appender.setName(name);
    appender.setAppend(true);
    appender.setFile(new File(getLogPath(), fileName).getAbsolutePath());

    TimeBasedRollingPolicy rolling = new TimeBasedRollingPolicy();
    rolling.setContext(loggerContext);
    rolling.setParent(appender);
    rolling.setFileNamePattern(new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd}");
    rolling.start();
    appender.setRollingPolicy(rolling);

    PatternLayoutEncoder layout = new PatternLayoutEncoder();
    layout.setContext(loggerContext);
    layout.setPattern(pattern);
    layout.setCharset(Charset.forName(getEncoding()));
    layout.start();
    appender.setEncoder(layout);
    // 启动
    appender.start();
    return appender;
}
 
Example #22
Source File: BaseTestCase.java    From AndroidMvc with Apache License 2.0 6 votes vote down vote up
private static void configureLogbackDirectly() {
    // reset the default context (which may already have been initialized)
    // since we want to reconfigure it
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    lc.reset();

    // setup LogcatAppender
    PatternLayoutEncoder encoder2 = new PatternLayoutEncoder();
    encoder2.setContext(lc);
    encoder2.setPattern("[%thread] %msg%n");
    encoder2.start();

    LogcatAppender logcatAppender = new LogcatAppender();
    logcatAppender.setContext(lc);
    logcatAppender.setEncoder(encoder2);
    logcatAppender.start();

    // backup the newly created appenders to the root logger;
    // qualify Logger to disambiguate from org.slf4j.Logger
    ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    root.addAppender(logcatAppender);

    root.setLevel(Level.ALL);
}
 
Example #23
Source File: ContainerLogger.java    From skywalking with Apache License 2.0 6 votes vote down vote up
public ContainerLogger(final Path logDirectory, final String container) {

        final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        final PatternLayoutEncoder encoder = new PatternLayoutEncoder();

        encoder.setPattern("%d{HH:mm:ss.SSS} %-5level %logger{36}.%M - %msg%n");
        encoder.setContext(context);
        encoder.start();

        final FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
        fileAppender.setFile(logDirectory.resolve(container).toAbsolutePath().toString());
        fileAppender.setEncoder(encoder);
        fileAppender.setContext(context);
        fileAppender.start();

        final ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(container);
        logger.addAppender(fileAppender);
        logger.setLevel(Level.DEBUG);
        logger.setAdditive(false);

        this.delegate = logger;
    }
 
Example #24
Source File: DynamicLogbackLogger.java    From tddl with Apache License 2.0 6 votes vote down vote up
private Appender buildAppender(String name, String fileName, String pattern) {
    RollingFileAppender appender = new RollingFileAppender();
    appender.setName(name);
    appender.setAppend(true);
    appender.setFile(new File(getLogPath(), fileName).getAbsolutePath());

    TimeBasedRollingPolicy rolling = new TimeBasedRollingPolicy();
    rolling.setParent(appender);
    rolling.setFileNamePattern(new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd}");
    appender.setRollingPolicy(rolling);

    PatternLayoutEncoder layout = new PatternLayoutEncoder();
    layout.setPattern(pattern);
    layout.setCharset(Charset.forName(getEncoding()));
    appender.setEncoder(layout);
    // 启动
    appender.start();
    return appender;
}
 
Example #25
Source File: SecurityTest.java    From owasp-security-logging with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
	// mockAppender = new RollingFileAppender();
	mockAppender.setContext(loggerContext);
	mockAppender.setFile("testFile.log");

	PatternLayoutEncoder encoder = new PatternLayoutEncoder();
	encoder.setContext(loggerContext);
	encoder.setPattern("%-4relative [%thread] %-5level %logger{35} - %msg%n");
	encoder.start();

	mockAppender.setEncoder(encoder);
	mockAppender.start();
	LOGGER = loggerContext.getLogger("Main");
	LOGGER.addAppender(mockAppender);

}
 
Example #26
Source File: Main.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
private static void setOutput(final String... paths) {
    LOG_ROOT.getLoggerContext().reset();

    final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setPattern("%date %level [%thread] [%file:%line] %msg%n");
    encoder.setContext(LOG_ROOT.getLoggerContext());
    encoder.start();

    for (final String path : paths) {
        // create FileAppender
        final FileAppender<ILoggingEvent> logfileOut = new FileAppender<>();
        logfileOut.setAppend(false);
        logfileOut.setFile(path);
        logfileOut.setContext(LOG_ROOT.getLoggerContext());
        logfileOut.setEncoder(encoder);
        logfileOut.start();

        // attach the rolling file appender to the root logger
        LOG_ROOT.addAppender(logfileOut);
    }
}
 
Example #27
Source File: Minister.java    From GOAi with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 初始化日志
 */
private Logger initLog(Integer id) {
    // 实例化log,每个实例存放地方不一致
    ch.qos.logback.classic.Logger logger = ((ch.qos.logback.classic.Logger)
            LoggerFactory.getLogger(this.secretary.strategyName + "-" + id));

    LoggerContext context = logger.getLoggerContext();

    TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<>();
    policy.setFileNamePattern(OptionHelper.substVars(
            "logs/past/" + id + "/%d{yyyy-MM-dd}.log.gz", context));
    policy.setMaxHistory(31);
    policy.setContext(context);

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(context);
    encoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} %5level - [%thread] %logger : %msg%n");

    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    appender.setContext(context);
    appender.setName(this.secretary.strategyName + "-" + id);
    appender.setFile(OptionHelper.substVars("logs/" + id + "/log.log", context));
    appender.setAppend(true);
    // 同一文件多输入完整检查
    appender.setPrudent(false);
    appender.setRollingPolicy(policy);
    appender.setEncoder(encoder);
    policy.setParent(appender);

    policy.start();
    encoder.start();
    appender.start();

    logger.setLevel(Level.INFO);
    // 终端输出
    logger.setAdditive(true);
    logger.addAppender(appender);

    return logger;
}
 
Example #28
Source File: LogUtils.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the appenders for command line.
 */
public static void setAppendersForCommandLine() {
    Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER);

    LoggerContext context = (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();

    rootLogger.detachAndStopAllAppenders();

    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();

    PatternLayoutEncoder consolePatternLayoutEncoder = new PatternLayoutEncoder();
    consolePatternLayoutEncoder.setPattern("%msg%n");
    consolePatternLayoutEncoder.setContext(context);
    consolePatternLayoutEncoder.start();

    consoleAppender.setEncoder(consolePatternLayoutEncoder);
    consoleAppender.start();

    rootLogger.addAppender(consoleAppender);

    ((Logger) LoggerFactory.getLogger("uk.org.lidalia")).setLevel(Level.ERROR);
    ((Logger) LoggerFactory.getLogger("org.nd4j")).setLevel(Level.ERROR);
    ((Logger) LoggerFactory.getLogger("org")).setLevel(Level.ERROR);
    ((Logger) LoggerFactory.getLogger("io")).setLevel(Level.ERROR);

    ((Logger) LoggerFactory.getLogger("ai")).setLevel(Level.INFO);
}
 
Example #29
Source File: WarServerInitializer.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Add a file appender to every logger we can find (the loggers should already have been configured via logback.xml)
 * 
 * @throws IOException
 */
private void fixLogging(BimServerConfig config) throws IOException {
	Path logFolder = config.getHomeDir().resolve("logs");
	if (!Files.isDirectory(logFolder)) {
		Files.createDirectories(logFolder);
	}
	Path file = logFolder.resolve("bimserver.log");

	LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
	PatternLayoutEncoder ple = new PatternLayoutEncoder();

	ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n");
	ple.setContext(lc);
	ple.start();
	FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>();
	String filename = file.toAbsolutePath().toString();

	if (lc instanceof LoggerContext) {
	    if (!lc.isStarted()) {
	    	lc.start();
	    }
	}
	
	System.out.println("Logging to " + filename);
	
	fileAppender.setFile(filename);
	fileAppender.setEncoder(ple);
	fileAppender.setContext(lc);
	fileAppender.start();

	for (ch.qos.logback.classic.Logger log : lc.getLoggerList()) {
		if (log.getLevel() != null) {
			log.addAppender(fileAppender);
		}
	}
}
 
Example #30
Source File: CombinatorialMaterializerTest.java    From tasmo with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void logger() {

    Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    LoggerContext loggerContext = rootLogger.getLoggerContext();
    loggerContext.reset();

    if (verbose) {

        PatternLayoutEncoder encoder = new PatternLayoutEncoder();
        encoder.setContext(loggerContext);
        encoder.setPattern("[%thread]: %message%n");
        encoder.start();

        ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
        appender.setContext(loggerContext);
        appender.setEncoder(encoder);
        appender.start();

        rootLogger.addAppender(appender);

        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo")).setLevel(Level.TRACE);
        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo.lib.concur.ConcurrencyAndExistanceCommitChange")).setLevel(Level.TRACE);
        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo.reference.lib.ReferenceStore")).setLevel(Level.TRACE);
        ((Logger) LoggerFactory.getLogger("com.jivesoftware.os.tasmo.view.reader.service.writer.WriteToViewValueStore")).setLevel(Level.TRACE);
    } else {

        rootLogger.setLevel(Level.OFF);
    }
}