Java Code Examples for ch.qos.logback.core.FileAppender#setAppend()

The following examples show how to use ch.qos.logback.core.FileAppender#setAppend() . 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: 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 2
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 3
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 4
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 5
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 6
Source File: LogbackUtils.java    From termsuite-core with Apache License 2.0 5 votes vote down vote up
public static OutputStreamAppender<ILoggingEvent> createFileAppender(Path path) {
	FileAppender<ILoggingEvent> fa = new FileAppender<ILoggingEvent>();
	fa.setFile(path.toString());
	fa.setAppend(true);
	fa.setName("FileLogger");
	return fa;
}
 
Example 7
Source File: LogUtils.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the file appender with the name of "FILE" if needed. If it's already been setup,
 * it would be ignored.
 */
public static void setAppendersForRunCommand(String serverId) {
    String logFilePath = new File(DirectoryFetcher.getCommandLogsDir(), serverId + ".log").getAbsolutePath();

    SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();

    Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER);

    rootLogger.detachAndStopAllAppenders();

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

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

    FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
    fileAppender.setName("FILE");
    fileAppender.setFile(logFilePath);
    fileAppender.setAppend(false);
    fileAppender.setContext(context);

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

    fileAppender.setEncoder(patternLayoutEncoder);
    fileAppender.start();

    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setName("CONSOLE");
    consoleAppender.setContext(context);
    consoleAppender.setEncoder(patternLayoutEncoder);
    consoleAppender.start();

    rootLogger.addAppender(fileAppender);
    rootLogger.addAppender(consoleAppender);

    log.info("Logging file at: {}", logFilePath);
}
 
Example 8
Source File: LogbackFactory.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
public static FileAppender<ILoggingEvent> buildFileAppender(LoggingConfiguration.FileConfiguration file,
                                                            LoggerContext context,
                                                            String logFormat) {
    final LogFormatter formatter = new LogFormatter(context, file.getTimeZone());

    if (logFormat != null) {
        formatter.setPattern(logFormat);
    }
    formatter.start();

    final FileAppender<ILoggingEvent> appender =
            file.isArchive() ? new RollingFileAppender<ILoggingEvent>() :
                    new FileAppender<ILoggingEvent>();

    appender.setAppend(true);
    appender.setContext(context);
    appender.setLayout(formatter);
    appender.setFile(file.getCurrentLogFilename());
    appender.setPrudent(false);

    addThresholdFilter(appender, file.getThreshold());

    if (file.isArchive()) {

        final DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> triggeringPolicy =
                new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
        triggeringPolicy.setContext(context);

        final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
        rollingPolicy.setContext(context);
        rollingPolicy.setFileNamePattern(file.getArchivedLogFilenamePattern());
        rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
        triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy);
        rollingPolicy.setMaxHistory(file.getArchivedFileCount());

        ((RollingFileAppender<ILoggingEvent>) appender).setRollingPolicy(rollingPolicy);
        ((RollingFileAppender<ILoggingEvent>) appender).setTriggeringPolicy(triggeringPolicy);

        rollingPolicy.setParent(appender);
        rollingPolicy.start();
    }

    appender.stop();
    appender.start();

    return appender;
}
 
Example 9
Source File: LogbackFactory.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
public static FileAppender<ILoggingEvent> buildFileAppender(LoggingConfiguration.FileConfiguration file,
                                                            LoggerContext context,
                                                            String logFormat) {
    final LogFormatter formatter = new LogFormatter(context, file.getTimeZone());

    if (logFormat != null) {
        formatter.setPattern(logFormat);
    }
    formatter.start();

    final FileAppender<ILoggingEvent> appender =
            file.isArchive() ? new RollingFileAppender<ILoggingEvent>() :
                    new FileAppender<ILoggingEvent>();

    appender.setAppend(true);
    appender.setContext(context);
    appender.setLayout(formatter);
    appender.setFile(file.getCurrentLogFilename());
    appender.setPrudent(false);

    addThresholdFilter(appender, file.getThreshold());

    if (file.isArchive()) {

        final DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent> triggeringPolicy =
                new DefaultTimeBasedFileNamingAndTriggeringPolicy<ILoggingEvent>();
        triggeringPolicy.setContext(context);

        final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>();
        rollingPolicy.setContext(context);
        rollingPolicy.setFileNamePattern(file.getArchivedLogFilenamePattern());
        rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
        triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy);
        rollingPolicy.setMaxHistory(file.getArchivedFileCount());

        ((RollingFileAppender<ILoggingEvent>) appender).setRollingPolicy(rollingPolicy);
        ((RollingFileAppender<ILoggingEvent>) appender).setTriggeringPolicy(triggeringPolicy);

        rollingPolicy.setParent(appender);
        rollingPolicy.start();
    }

    appender.stop();
    appender.start();

    return appender;
}