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

The following examples show how to use ch.qos.logback.core.FileAppender#setName() . 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: 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 4
Source File: LogUtils.java    From konduit-serving with Apache License 2.0 5 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 setFileAppenderIfNeeded() {
    SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();

    File previousLogsFile = getEndpointLogsFile();

    File newLogsFile = new File(DirectoryFetcher.getEndpointLogsDir(), Constants.MAIN_ENDPOINT_LOGS_FILE);

    if(!newLogsFile.equals(previousLogsFile)) {
        Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER);

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

        if(previousLogsFile != null) {
            rootLogger.detachAppender("FILE");
        }

        FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
        fileAppender.setName("FILE");
        fileAppender.setFile(newLogsFile.getAbsolutePath());
        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();

        rootLogger.addAppender(fileAppender);
    }
}
 
Example 5
Source File: AccountImpl.java    From java-trader with Apache License 2.0 5 votes vote down vote up
/**
 * 创建Account Logger
 * @param separateLogger true 会为每个Account创建一个日志文件: TRADER_HOME/work/<TRADING_DAY>/accountId.log
 */
private void createAccountLogger() {
    if ( !simMode ) {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

        FileAppender fileAppender = new FileAppender();
        fileAppender.setContext(loggerContext);
        fileAppender.setName("timestamp");
        // set the file name
        File logFile = new File(tradingWorkDir, id+".log");
        fileAppender.setFile(logFile.getAbsolutePath());

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

        fileAppender.setEncoder(encoder);
        fileAppender.start();

        // attach the rolling file appender to the logger of your choice
        loggerCategory = AccountImpl.class.getName()+"."+id;
        ch.qos.logback.classic.Logger packageLogger = loggerContext.getLogger(loggerCategory);
        packageLogger.addAppender(fileAppender);
        packageLogger.setAdditive(true); //保证每个Account数据, 在主的日志中也有一份

        logger = loggerContext.getLogger(loggerCategory+"."+AccountImpl.class.getSimpleName());
    } else {
        loggerCategory = AccountImpl.class.getName();
        logger = LoggerFactory.getLogger(AccountImpl.class);
    }
}
 
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: LogbackConfiguration.java    From IridiumApplicationTesting with MIT License 4 votes vote down vote up
@Override
public void configureLogging(@NotNull final String logfile) {
	checkArgument(StringUtils.isNotBlank(logfile));

	try {
		final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

		final FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
		fileAppender.setContext(loggerContext);
		fileAppender.setName("timestamp");
		// set the file name
		fileAppender.setFile(logfile);

		final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
		encoder.setContext(loggerContext);
		encoder.setPattern("%r %thread %level - %msg%n");
		encoder.start();

		fileAppender.setEncoder(encoder);
		fileAppender.start();

		// attach the rolling file appender to the logger of your choice
		final Logger logbackLogger =
			(Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
		logbackLogger.addAppender(fileAppender);

		/*
			We only want to hear about errors from the libraries that are used by Iridium
		 */
		logbackLogger.setLevel(Level.ERROR);

		/*
			Our own code should be info level
		 */
		final Logger iridiumLogger =
			(Logger) LoggerFactory.getLogger("au.com.agic");
		iridiumLogger.setLevel(Level.INFO);

		/*
			Performance increase for redirected JUL loggers
		 */
		final LevelChangePropagator levelChangePropagator = new LevelChangePropagator();
		levelChangePropagator.setContext(loggerContext);
		levelChangePropagator.setResetJUL(true);
		loggerContext.addListener(levelChangePropagator);

		/*
			Redirect java logging and sys out to slf4j
		 */
		LogManager.getLogManager().reset();
		SLF4JBridgeHandler.removeHandlersForRootLogger();
		SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();
		SLF4JBridgeHandler.install();
	} catch (final Exception ex) {
		LOGGER.error("WEBAPPTESTER-BUG-0006: Could not configure Logback", ex);
	}
}