Java Code Examples for ch.qos.logback.classic.Logger#setAdditive()
The following examples show how to use
ch.qos.logback.classic.Logger#setAdditive() .
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: LogbackUtil.java From summerframework with Apache License 2.0 | 6 votes |
private static Logger build(String name, Level logLevel, String fileName) { LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory(); Logger logger = context.getLogger(name); if (Level.ALL.equals(logLevel)) { RollingFileAppender errorAppender = new AppenderBuild().getAppender(name, Level.ERROR, fileName); RollingFileAppender infoAppender = new AppenderBuild().getAppender(name, Level.INFO, fileName); RollingFileAppender warnAppender = new AppenderBuild().getAppender(name, Level.WARN, fileName); RollingFileAppender debugAppender = new AppenderBuild().getAppender(name, Level.DEBUG, fileName); logger.setAdditive(false); logger.addAppender(errorAppender); logger.addAppender(infoAppender); logger.addAppender(warnAppender); logger.addAppender(debugAppender); } else { logger = buildInfoByLevel(name, logLevel, fileName); } return logger; }
Example 2
Source File: LoggerMenu.java From Hive2Hive with MIT License | 6 votes |
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 3
Source File: LogbackController.java From GreenSummer with GNU Lesser General Public License v2.1 | 6 votes |
/** * * @param name The name of the logger we want to manipulate the level of * @param level The new log level * @return The list of log levels configured and their settings */ @RequestMapping(value = "set/{name}/{level}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET, headers = "Accept=application/json") @ResponseBody public ResponseEntity<LogResponse> set(@PathVariable("name") final String name, @PathVariable("level") final String level) { final LoggerContext ctx = (LoggerContext) LoggerFactory.getILoggerFactory(); synchronized (ctx) { Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME); Logger logger = ctx.getLogger(name); if (logger != null) { logger.setLevel(Level.toLevel(level)); logger.setAdditive(false); for (Iterator<Appender<ILoggingEvent>> it = root.iteratorForAppenders(); it.hasNext();) { logger.addAppender(it.next()); } } } return new ResponseEntity<>(listLoggers(ctx), HttpStatus.OK); }
Example 4
Source File: LogbackController.java From GreenSummer with GNU Lesser General Public License v2.1 | 6 votes |
/** * Captures the given logger at the given level so it can be displayed directly * by this controller. * * @param name The name of the logger we want to capture * @param level The level of logging we want to capture * @param append Where we want to also send the logs to the regular pipes (true) or just capture it (false, default) * @return The list of log levels configured and their settings */ @RequestMapping(value = "capture/{name}/{level}", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET, headers = "Accept=application/json") @ResponseBody public ResponseEntity<LogResponse> capture(@PathVariable("name") final String name, @PathVariable("level") final String level, @RequestParam(value = "append", defaultValue = "false") boolean append) { final LoggerContext ctx = (LoggerContext) LoggerFactory.getILoggerFactory(); synchronized (ctx) { if (logbackMemoryAppenderImpl != null) { Logger logger = ctx.getLogger(name); if (logger != null) { logger.setLevel(Level.toLevel(level)); logger.setAdditive(append); logger.addAppender(logbackMemoryAppenderImpl); } } } return new ResponseEntity<>(listLoggers(ctx), HttpStatus.OK); }
Example 5
Source File: LogbackContainer.java From dubbox with Apache License 2.0 | 5 votes |
/** * Initializer logback * * @param file * @param level * @param maxHistory */ private void doInitializer(String file, String level, int maxHistory) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.detachAndStopAllAppenders(); // appender RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); fileAppender.setContext(loggerContext); fileAppender.setName("application"); fileAppender.setFile(file); fileAppender.setAppend(true); // policy TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>(); policy.setContext(loggerContext); policy.setMaxHistory(maxHistory); policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}"); policy.setParent(fileAppender); policy.start(); fileAppender.setRollingPolicy(policy); // encoder PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n"); encoder.start(); fileAppender.setEncoder(encoder); fileAppender.start(); rootLogger.addAppender(fileAppender); rootLogger.setLevel(Level.toLevel(level)); rootLogger.setAdditive(false); }
Example 6
Source File: LogbackContainer.java From dubbox with Apache License 2.0 | 5 votes |
/** * Initializer logback * * @param file * @param level * @param maxHistory */ private void doInitializer(String file, String level, int maxHistory) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.detachAndStopAllAppenders(); // appender RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); fileAppender.setContext(loggerContext); fileAppender.setName("application"); fileAppender.setFile(file); fileAppender.setAppend(true); // policy TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>(); policy.setContext(loggerContext); policy.setMaxHistory(maxHistory); policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}"); policy.setParent(fileAppender); policy.start(); fileAppender.setRollingPolicy(policy); // encoder PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n"); encoder.start(); fileAppender.setEncoder(encoder); fileAppender.start(); rootLogger.addAppender(fileAppender); rootLogger.setLevel(Level.toLevel(level)); rootLogger.setAdditive(false); }
Example 7
Source File: LogbackSlowQueryLogProvider.java From emodb with Apache License 2.0 | 5 votes |
@Override public SlowQueryLog get() { Logger logger = (Logger) LoggerFactory.getLogger("sor.slow-query"); logger.setAdditive(false); LoggerContext context = logger.getLoggerContext(); logger.detachAndStopAllAppenders(); PatternLayout logPatternLayout = new PatternLayout(); logPatternLayout.setPattern("%-5p [%d{ISO8601," + _config.getTimeZone().getID() + "}] %m\n"); logPatternLayout.setContext(context); logPatternLayout.start(); ConsoleAppenderFactory consoleAppenderFactory = _config.getConsoleAppenderFactory(); if (consoleAppenderFactory != null) { // Console is usually used only in development. Use a synchronous appender so console output doesn't get re-ordered. logger.addAppender(consoleAppenderFactory.build(context, EMODB_APPLICATION_NAME, logPatternLayout)); } FileAppenderFactory fileAppenderFactory = _config.getFileAppenderFactory(); if (fileAppenderFactory != null) { AsyncAppender fileAsyncAppender = new AsyncAppender(); fileAsyncAppender.addAppender(fileAppenderFactory.build(context, EMODB_APPLICATION_NAME, logPatternLayout)); fileAsyncAppender.start(); logger.addAppender(fileAsyncAppender); } SyslogAppenderFactory syslogAppenderFactory = _config.getSyslogAppenderFactory(); if (syslogAppenderFactory != null) { AsyncAppender sysAsyncAppender = new AsyncAppender(); sysAsyncAppender.addAppender(syslogAppenderFactory.build(context, EMODB_APPLICATION_NAME, logPatternLayout)); sysAsyncAppender.start(); logger.addAppender(sysAsyncAppender); } return new LogbackSlowQueryLog(logger, _config.getTooManyDeltasThreshold(), _metricRegistry); }
Example 8
Source File: LogbackContainer.java From dubbox with Apache License 2.0 | 5 votes |
/** * Initializer logback * * @param file * @param level * @param maxHistory */ private void doInitializer(String file, String level, int maxHistory) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.detachAndStopAllAppenders(); // appender RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); fileAppender.setContext(loggerContext); fileAppender.setName("application"); fileAppender.setFile(file); fileAppender.setAppend(true); // policy TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>(); policy.setContext(loggerContext); policy.setMaxHistory(maxHistory); policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}"); policy.setParent(fileAppender); policy.start(); fileAppender.setRollingPolicy(policy); // encoder PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n"); encoder.start(); fileAppender.setEncoder(encoder); fileAppender.start(); rootLogger.addAppender(fileAppender); rootLogger.setLevel(Level.toLevel(level)); rootLogger.setAdditive(false); }
Example 9
Source File: GelfTcpTlsAppenderTest.java From logback-gelf with GNU Lesser General Public License v2.1 | 5 votes |
private Logger setupLogger() { final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); final GelfEncoder gelfEncoder = new GelfEncoder(); gelfEncoder.setContext(lc); gelfEncoder.setOriginHost("localhost"); gelfEncoder.start(); final Logger logger = (Logger) LoggerFactory.getLogger(LOGGER_NAME); logger.addAppender(buildAppender(lc, gelfEncoder)); logger.setAdditive(false); return logger; }
Example 10
Source File: GelfTcpAppenderTest.java From logback-gelf with GNU Lesser General Public License v2.1 | 5 votes |
private Logger setupLogger() { final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); final GelfEncoder gelfEncoder = new GelfEncoder(); gelfEncoder.setContext(lc); gelfEncoder.setOriginHost("localhost"); gelfEncoder.start(); final Logger logger = (Logger) LoggerFactory.getLogger(LOGGER_NAME); logger.addAppender(buildAppender(lc, gelfEncoder)); logger.setAdditive(false); return logger; }
Example 11
Source File: LogbackContainer.java From dubbo3 with Apache License 2.0 | 5 votes |
/** * Initializer logback * * @param file log file * @param level log level * @param maxHistory max history */ private void doInitializer(String file, String level, int maxHistory) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.detachAndStopAllAppenders(); // appender RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<>(); fileAppender.setContext(loggerContext); fileAppender.setName("application"); fileAppender.setFile(file); fileAppender.setAppend(true); // policy TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<>(); policy.setContext(loggerContext); policy.setMaxHistory(maxHistory); policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}"); policy.setParent(fileAppender); policy.start(); fileAppender.setRollingPolicy(policy); // encoder PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n"); encoder.start(); fileAppender.setEncoder(encoder); fileAppender.start(); rootLogger.addAppender(fileAppender); rootLogger.setLevel(Level.toLevel(level)); rootLogger.setAdditive(false); }
Example 12
Source File: LogbackContainer.java From dubbox-hystrix with Apache License 2.0 | 5 votes |
/** * Initializer logback * * @param file * @param level * @param maxHistory */ private void doInitializer(String file, String level, int maxHistory) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.detachAndStopAllAppenders(); // appender RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); fileAppender.setContext(loggerContext); fileAppender.setName("application"); fileAppender.setFile(file); fileAppender.setAppend(true); // policy TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>(); policy.setContext(loggerContext); policy.setMaxHistory(maxHistory); policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}"); policy.setParent(fileAppender); policy.start(); fileAppender.setRollingPolicy(policy); // encoder PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n"); encoder.start(); fileAppender.setEncoder(encoder); fileAppender.start(); rootLogger.addAppender(fileAppender); rootLogger.setLevel(Level.toLevel(level)); rootLogger.setAdditive(false); }
Example 13
Source File: LogbackContainer.java From dubbo-2.6.5 with Apache License 2.0 | 5 votes |
/** * Initializer logback * * @param file * @param level * @param maxHistory */ private void doInitializer(String file, String level, int maxHistory) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.detachAndStopAllAppenders(); // appender RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); fileAppender.setContext(loggerContext); fileAppender.setName("application"); fileAppender.setFile(file); fileAppender.setAppend(true); // policy TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>(); policy.setContext(loggerContext); policy.setMaxHistory(maxHistory); policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}"); policy.setParent(fileAppender); policy.start(); fileAppender.setRollingPolicy(policy); // encoder PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n"); encoder.start(); fileAppender.setEncoder(encoder); fileAppender.start(); rootLogger.addAppender(fileAppender); rootLogger.setLevel(Level.toLevel(level)); rootLogger.setAdditive(false); }
Example 14
Source File: MaruLoggerFactory.java From MMDownloader with Apache License 2.0 | 5 votes |
/** * 에러 내용을 파일로 저장하는 Logger 를 가져온다. * * @param logFileName 저장될 로그파일의 절대경로 * @return 에러 출력 & 저장용 Logger 객체 */ public static Logger getErrorLogger(String logFileName) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); PatternLayoutEncoder encoder = getEncoder(loggerContext, "[%d{yyyy-MM-dd HH:mm:ss}][%-5level][%thread][%class{0}:%line] %msg%n"); String loggerName = "ErrorLogger"; FileAppender fileAppender = getFileAppender(loggerContext, encoder, logFileName, loggerName); Logger logger = loggerContext.getLogger(loggerName); logger.addAppender(fileAppender); logger.setAdditive(false); return logger; }
Example 15
Source File: LoggerBuilder.java From nuls-v2 with MIT License | 5 votes |
@SuppressWarnings("unchecked") private static NulsLogger build(String fileName, Level fileLevel, Level consoleLevel) { RollingFileAppender fileAppender = LogAppender.getAppender(fileName, fileLevel); LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); Logger logger = context.getLogger(fileAppender.getEncoder().toString()); //设置不向上级打印信息 logger.setAdditive(false); logger.addAppender(fileAppender); //输出到控制台 Appender consoleAppender = LogAppender.createConsoleAppender(consoleLevel); logger.addAppender(consoleAppender); return new NulsLogger(logger); }
Example 16
Source File: TestRunner.java From AuTe-Framework with Apache License 2.0 | 5 votes |
private void prepareLoggers() { Set<String> loggers = new HashSet<>(Arrays.asList( "org.apache.http", "org.apache.commons.beanutils.converters" )); for (String log : loggers) { Logger logger = (Logger) LoggerFactory.getLogger(log); logger.setLevel(Level.WARN); logger.setAdditive(false); } }
Example 17
Source File: LogbackUtil.java From summerframework with Apache License 2.0 | 5 votes |
private static Logger buildInfoByLevel(String name, Level logLevel, String fileName) { RollingFileAppender appender = new AppenderBuild().getAppender(name, logLevel, fileName); LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory(); Logger logger = context.getLogger(name); logger.setAdditive(false); logger.addAppender(appender); return logger; }
Example 18
Source File: DiagnosticLogging.java From hivemq-community-edition with Apache License 2.0 | 5 votes |
/** * Sets the trace log to the root logger. Also adds filter, to make sure that * the appender which are already defined for HiveMQ are not affected by this logging * level change. * <p> * <b>This will significantly slow down HiveMQ, since the root level loggers Level is changed * to the finest logging level!</b> * * @param filePath the file path */ static void setTraceLog(final String filePath) { log.info("Creating trace log {}", filePath); final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); final Level originalLoggingLevel = logger.getLevel(); final Iterator<Appender<ILoggingEvent>> appenderIterator = logger.iteratorForAppenders(); while (appenderIterator.hasNext()) { final Appender<ILoggingEvent> next = appenderIterator.next(); next.addFilter(new PreserveOriginalLoggingLevelFilter(originalLoggingLevel)); } final LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); final PatternLayoutEncoder ple = new PatternLayoutEncoder(); ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n"); ple.setContext(lc); ple.start(); final FileAppender<ILoggingEvent> fileAppender = new FileAppender<>(); fileAppender.setFile(filePath); fileAppender.setEncoder(ple); fileAppender.setContext(lc); fileAppender.start(); logger.addAppender(fileAppender); logger.setLevel(Level.ALL); logger.setAdditive(false); }
Example 19
Source File: LogbackLogManager.java From seed with Mozilla Public License 2.0 | 4 votes |
@Override public synchronized void configure(LoggingConfig loggingConfig) { if (context.isStarted()) { context.stop(); } context.reset(); context.start(); boolean autoConfigurationFailed = false; try { new ContextInitializer(context).autoConfig(); } catch (JoranException e) { autoConfigurationFailed = true; } if (autoConfigurationFailed || !isExplicitlyConfigured()) { context.reset(); // Root logger level Logger rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.setLevel(convertLevel(loggingConfig.getLevel())); // Configure configureConsole(loggingConfig.console(), rootLogger); configureFile(loggingConfig.file(), rootLogger); // Nuun is quite verbose so it is set to WARN by default Logger nuunLogger = context.getLogger("io.nuun"); nuunLogger.setLevel(Level.WARN); // When running under Tomcat with a LevelChangePropagator, DEBUG level and below lead to an exception // so we force INFO level if (underTomcat && (loggingConfig.getLevel() == LoggingConfig.Level.DEBUG || loggingConfig.getLevel() == LoggingConfig.Level.TRACE)) { context.getLogger("org.apache.catalina").setLevel(Level.INFO); context.getLogger("org.apache.juli").setLevel(Level.INFO); } // Configure explicit loggers for (Map.Entry<String, LoggingConfig.LoggerConfig> loggerLevelEntry : loggingConfig.loggers() .entrySet()) { Logger logger = context.getLogger(loggerLevelEntry.getKey()); LoggingConfig.LoggerConfig config = loggerLevelEntry.getValue(); logger.setLevel(convertLevel(config.getLevel())); logger.setAdditive(config.isAdditive()); } // Add level propagator for performance of JUL LevelChangePropagator levelChangePropagator = new LevelChangePropagator(); levelChangePropagator.setContext(context); levelChangePropagator.setResetJUL(true); context.addListener(levelChangePropagator); levelChangePropagator.start(); } }
Example 20
Source File: LogbackConfiguration.java From portmapper with GNU General Public License v3.0 | 4 votes |
@SuppressWarnings("java:S4792") // Logger configuration is ok private void configureLogger(final OutputStreamAppender<ILoggingEvent> appender) { final Logger logbackLogger = getLogger(); logbackLogger.addAppender(appender); logbackLogger.setAdditive(false); }