Java Code Examples for ch.qos.logback.classic.encoder.PatternLayoutEncoder#setContext()
The following examples show how to use
ch.qos.logback.classic.encoder.PatternLayoutEncoder#setContext() .
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: Utils.java From jlineup with Apache License 2.0 | 6 votes |
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 2
Source File: LogbackLogConfigurator.java From butterfly with MIT License | 6 votes |
@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 3
Source File: SecurityTest.java From owasp-security-logging with Apache License 2.0 | 6 votes |
@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 4
Source File: StartupAppender.java From qpid-broker-j with Apache License 2.0 | 6 votes |
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 5
Source File: Emissary.java From emissary with Apache License 2.0 | 6 votes |
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 6
Source File: KonkerLoggerBasicConfigurator.java From konker-platform with Apache License 2.0 | 6 votes |
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 7
Source File: LoggingConfigurator.java From logging-java with Apache License 2.0 | 6 votes |
/** * Create a stderr appender. * * @param context The logger context to use. * @return An appender writing to stderr. */ private static Appender<ILoggingEvent> getStdErrAppender(final LoggerContext context, final ReplaceNewLines replaceNewLines) { // Setup format final PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(context); encoder.setPattern( "%date{HH:mm:ss.SSS} %property{ident}[%property{pid}]: %-5level [%thread] %logger{0}: " + ReplaceNewLines.getMsgPattern(replaceNewLines) + "%n"); encoder.setCharset(Charsets.UTF_8); encoder.start(); // Setup stderr appender final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>(); appender.setTarget("System.err"); appender.setName("stderr"); appender.setEncoder(encoder); appender.setContext(context); appender.start(); return appender; }
Example 8
Source File: DynamicLogbackLogger.java From tddl5 with Apache License 2.0 | 6 votes |
protected Appender buildDailyMaxRollingAppender(String name, String fileName, String pattern, int maxBackupIndex) { 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.setFileNamePattern(new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd-HH}"); rolling.setMaxHistory(maxBackupIndex); rolling.setParent(appender); 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 9
Source File: LoggingTestWatcher.java From helios with Apache License 2.0 | 5 votes |
/** * Sets up a FileAppender under the path {@code $logDir/<timestamp>-<name>-<pid>.log}. If not set * as a system property then {@code $logDir} falls back to {@code /tmp/helios-test/log}. */ private void setupFileLogging(final String name) { final ch.qos.logback.classic.Logger rootLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME); final LoggerContext context = rootLogger.getLoggerContext(); context.reset(); final String ts = new SimpleDateFormat("yyyyMMdd'T'HHmmss.SSS").format(new Date()); final String pid = ManagementFactory.getRuntimeMXBean().getName().split("@", 2)[0]; final PatternLayoutEncoder ple = new PatternLayoutEncoder(); ple.setContext(context); ple.setPattern("%d{HH:mm:ss.SSS} %-5level %logger{1} %F:%L - %msg%n"); ple.start(); final Path directory = Paths.get(System.getProperty("logDir", "/tmp/helios-test/log/")); final String filename = String.format("%s-%s-%s.log", ts, name, pid); final Path file = directory.resolve(filename); final FileAppender<ILoggingEvent> fileAppender = new FileAppender<>(); fileAppender.setEncoder(ple); fileAppender.setFile(file.toString()); fileAppender.setContext(context); fileAppender.start(); rootLogger.setLevel(Level.DEBUG); rootLogger.addAppender(fileAppender); try { Files.createDirectories(directory); } catch (IOException e) { throw new RuntimeException(e); } configureLogger("org.eclipse.jetty", Level.ERROR); configureLogger("org.apache.curator", Level.ERROR); configureLogger("org.apache.zookeeper", Level.ERROR); configureLogger("com.spotify.helios", Level.DEBUG); }
Example 10
Source File: LogUtils.java From konduit-serving with Apache License 2.0 | 5 votes |
/** * 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 11
Source File: TestLogAppender.java From karate with MIT License | 5 votes |
public TestLogAppender() { sb = new StringBuilder(); logger = (Logger) LoggerFactory.getLogger("com.intuit.karate"); setName("karate-test"); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); setContext(lc); encoder = new PatternLayoutEncoder(); encoder.setPattern("%msg"); encoder.setContext(context); encoder.start(); start(); logger.addAppender(this); logger.setLevel(Level.DEBUG); }
Example 12
Source File: LogUtil.java From obevo with Apache License 2.0 | 5 votes |
private FileAppender<ILoggingEvent> createAppender(String logFilePath) { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); PatternLayoutEncoder ple = new PatternLayoutEncoder(); ple.setPattern("[%p] %c{1} [%t] %d{[M-dd HH:mm:ss]} - %m%n"); ple.setContext(lc); ple.start(); FileAppender<ILoggingEvent> fileAppender = new FileAppender<>(); fileAppender.setFile(logFilePath); fileAppender.setEncoder(ple); fileAppender.setContext(lc); fileAppender.start(); return fileAppender; }
Example 13
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 14
Source File: LogbackUtils.java From termsuite-core with Apache License 2.0 | 5 votes |
static void activateLogging(OutputStreamAppender<ILoggingEvent> appender) { PatternLayoutEncoder ple = new PatternLayoutEncoder(); ple.setPattern("%date %level [%thread] %logger{10} [%file:%line] %msg%n"); ple.setContext(getContext()); ple.start(); appender.setContext(getContext()); appender.setEncoder(ple); appender.start(); // add appender to any Logger (here is root) ch.qos.logback.classic.Logger rootLogger = getRootLogger(); rootLogger.addAppender(appender); }
Example 15
Source File: LogWatcher.java From brooklyn-server with Apache License 2.0 | 4 votes |
public LogWatcher(Iterable<String> loggerNames, ch.qos.logback.classic.Level loggerLevel, final Predicate<? super ILoggingEvent> filter) { this.loggerLevel = checkNotNull(loggerLevel, "loggerLevel"); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); this.appender = new ConsoleAppender<>(); PatternLayoutEncoder ple = new PatternLayoutEncoder() { @Override public byte[] encode(ILoggingEvent event) { final String txt = layout.doLayout(event); // Jump through hoops to turn the input event (without any layout) // into one processed by the pattern layout, prior to applying the filter. // Wrap the input event in a dynamic proxy to lay out the message/toString methods // but delegate all other methods to the real input event ILoggingEvent formatted = (ILoggingEvent) Proxy.newProxyInstance( ILoggingEvent.class.getClassLoader(), new Class<?>[]{ILoggingEvent.class}, (proxy, method, args) -> { if (method.getName().endsWith("Message") || method.getName().equals("toString")) { return txt; } else { return method.invoke(event, args); } }); // now we can do the filter on the text that will be written to the log output if (event != null && filter.apply(formatted)) { events.add(formatted); } LOG.trace("level={}; event={}; msg={}", event.getLevel(), event, event.getFormattedMessage()); return super.encode(event); } }; // The code below makes the assumption that the (test) logger configuration has a console appender // for root, with a pattern layout encoder, and re-uses its encoder pattern. // This is (at time of writing) as defined in logback-appender-stdout.xml. final Appender<ILoggingEvent> appender = lc.getLogger("ROOT").getAppender("STDOUT"); final ConsoleAppender<?> stdout = ConsoleAppender.class.cast(appender); final PatternLayoutEncoder stdoutEncoder = PatternLayoutEncoder.class.cast(stdout.getEncoder()); ple.setPattern(stdoutEncoder.getPattern()); ple.setContext(lc); ple.start(); this.appender.setContext(lc); this.appender.setEncoder(ple); this.appender.start(); for (String loggerName : loggerNames) { watchedLoggers.add( (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(checkNotNull(loggerName, "loggerName")) ); } start(); }
Example 16
Source File: AthenaAuditLoggerConfiguration.java From cerberus with Apache License 2.0 | 4 votes |
@Autowired public AthenaAuditLoggerConfiguration( @Value("${cerberus.audit.athena.log.path:#{null}}") String logPath, AuditLogsS3TimeBasedRollingPolicy<ILoggingEvent> auditLogsS3TimeBasedRollingPolicy) { if (StringUtils.isBlank(logPath)) { logPath = ""; } else if (!logPath.endsWith("/")) { logPath += "/"; FilenameUtils.getPath( logPath); // this shouldn't be necessary because the path is provided by Spring config, // but extra safety } this.auditLogsS3TimeBasedRollingPolicy = auditLogsS3TimeBasedRollingPolicy; LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder(); patternLayoutEncoder.setPattern(MESSAGE_PATTERN); patternLayoutEncoder.setContext(loggerContext); patternLayoutEncoder.start(); String hostname; try { hostname = System.getenv("HOSTNAME") != null ? System.getenv("HOSTNAME") : InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { throw new RuntimeException("Unable to find host name"); } FiveMinuteRollingFileAppender<ILoggingEvent> fiveMinuteRollingFileAppender = new FiveMinuteRollingFileAppender<>(); fiveMinuteRollingFileAppender.setName(ATHENA_LOG_APPENDER_NAME); fiveMinuteRollingFileAppender.setContext(loggerContext); fiveMinuteRollingFileAppender.setFile(logPath + hostname + "-audit.log"); fiveMinuteRollingFileAppender.setEncoder(patternLayoutEncoder); this.auditLogsS3TimeBasedRollingPolicy.setContext(loggerContext); this.auditLogsS3TimeBasedRollingPolicy.setFileNamePattern( logPath + hostname + "-audit.%d{yyyy-MM-dd_HH-mm, UTC}.log.gz"); this.auditLogsS3TimeBasedRollingPolicy.setMaxHistory(100); this.auditLogsS3TimeBasedRollingPolicy.setParent(fiveMinuteRollingFileAppender); this.auditLogsS3TimeBasedRollingPolicy.setTotalSizeCap(FileSize.valueOf("10gb")); fiveMinuteRollingFileAppender.setTriggeringPolicy(this.auditLogsS3TimeBasedRollingPolicy); fiveMinuteRollingFileAppender.setRollingPolicy(this.auditLogsS3TimeBasedRollingPolicy); this.auditLogsS3TimeBasedRollingPolicy.start(); fiveMinuteRollingFileAppender.start(); var logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(ATHENA_AUDIT_LOGGER_NAME); logger.addAppender(fiveMinuteRollingFileAppender); logger.setLevel(Level.INFO); logger.setAdditive(false); athenaAuditLogger = logger; }
Example 17
Source File: FlickrUploader.java From flickr-uploader with GNU General Public License v2.0 | 4 votes |
private static void initLogs() { Logger logbackLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); LoggerContext lc = logbackLogger.getLoggerContext(); Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.detachAndStopAllAppenders(); TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>(); rollingPolicy.setMaxHistory(3); SizeAndTimeBasedFNATP<ILoggingEvent> sizeAndTimeBasedFNATP = new SizeAndTimeBasedFNATP<ILoggingEvent>(); sizeAndTimeBasedFNATP.setMaxFileSize("2MB"); rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(sizeAndTimeBasedFNATP); rollingPolicy.setFileNamePattern(context.getFilesDir().getPath() + "/logs/old/flickruploader.%d{yyyy-MM-dd}.%i.log"); rollingPolicy.setContext(lc); RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); fileAppender.setContext(lc); fileAppender.setFile(getLogFilePath()); fileAppender.setRollingPolicy(rollingPolicy); fileAppender.setTriggeringPolicy(rollingPolicy); rollingPolicy.setParent(fileAppender); PatternLayoutEncoder pl = new PatternLayoutEncoder(); pl.setContext(lc); pl.setCharset(Charset.defaultCharset()); pl.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %class{0}.%method:%L > %msg%n"); pl.setImmediateFlush(false); pl.start(); fileAppender.setEncoder(pl); fileAppender.setName("file"); rollingPolicy.start(); fileAppender.start(); if (Config.isDebug()) { final PatternLayoutEncoder logcatTagPattern = new PatternLayoutEncoder(); logcatTagPattern.setContext(lc); logcatTagPattern.setPattern("%class{0}"); logcatTagPattern.start(); final PatternLayoutEncoder logcatPattern = new PatternLayoutEncoder(); logcatPattern.setContext(lc); logcatPattern.setPattern("[%thread] %method:%L > %msg%n"); logcatPattern.start(); final LogcatAppender logcatAppender = new LogcatAppender(); logcatAppender.setContext(lc); logcatAppender.setTagEncoder(logcatTagPattern); logcatAppender.setEncoder(logcatPattern); logcatAppender.start(); rootLogger.addAppender(logcatAppender); } rootLogger.addAppender(fileAppender); }
Example 18
Source File: LogAppender.java From nuls-v2 with MIT License | 4 votes |
/** * 通过传入的名字和级别,动态设置appender * * @param fileName * @return */ @SuppressWarnings("unchecked") public static RollingFileAppender getAppender(String fileName, Level level){ String rootPath = PROJECT_PATH; if(!rootPath.endsWith(File.separator)){ rootPath += File.separator; } if(fileName.startsWith(File.separator)){ fileName = fileName.substring(1); } LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); RollingFileAppender appender = new RollingFileAppender(); /*设置上下文,每个logger都关联到logger上下文,默认上下文名称为default。 但可以使用<contextName>设置成其他名字,用于区分不同应用程序的记录。一旦设置,不能修改。*/ appender.setContext(context); //这里设置级别过滤器 LogFilter levelController = new LogFilter(); ThresholdFilter levelFilter = levelController.getThresholdFilter(level); levelFilter.start(); appender.addFilter(levelFilter); //设置文件名 appender.setFile(OptionHelper.substVars(rootPath+fileName + ".log",context)); appender.setAppend(true); appender.setPrudent(false); //设置文件创建时间及大小的类 SizeAndTimeBasedRollingPolicy policy = new SizeAndTimeBasedRollingPolicy(); //文件名格式 String fp = OptionHelper.substVars(rootPath+ fileName + ".%d{yyyy-MM-dd}.%i.zip",context); //最大日志文件大小 policy.setMaxFileSize(FileSize.valueOf("100MB")); //设置文件名模式 policy.setFileNamePattern(fp); //设置保存最近3天的日志 policy.setMaxHistory(3); //总大小限制 policy.setContext(context); policy.setTotalSizeCap(FileSize.valueOf("1GB")); //设置父节点是appender policy.setParent(appender); //设置上下文,每个logger都关联到logger上下文,默认上下文名称为default。 //但可以使用<contextName>设置成其他名字,用于区分不同应用程序的记录。一旦设置,不能修改。 policy.start(); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); //设置上下文,每个logger都关联到logger上下文,默认上下文名称为default。 //但可以使用<contextName>设置成其他名字,用于区分不同应用程序的记录。一旦设置,不能修改。 encoder.setContext(context); //设置格式 /*encoder.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%t] %replace(%caller{1}){'\\t|Caller.{1}0|\\r\\n', ''} - %msg%n");*/ encoder.setPattern("%d %p [%t] - %msg%n"); encoder.start(); //加入下面两个节点 appender.setRollingPolicy(policy); appender.setEncoder(encoder); appender.start(); return appender; }
Example 19
Source File: BitherApplication.java From bither-android with Apache License 2.0 | 4 votes |
private void initLogging() { final File logDir = getLogDir(); final File logFile = new File(logDir, "bitherj.log"); final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); final PatternLayoutEncoder filePattern = new PatternLayoutEncoder(); filePattern.setContext(context); filePattern.setPattern("%d{HH:mm:ss.SSS} [%thread] %logger{0} - %msg%n"); filePattern.start(); final RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>(); fileAppender.setContext(context); fileAppender.setFile(logFile.getAbsolutePath()); final TimeBasedRollingPolicy<ILoggingEvent> rollingPolicy = new TimeBasedRollingPolicy<ILoggingEvent>(); rollingPolicy.setContext(context); rollingPolicy.setParent(fileAppender); rollingPolicy.setFileNamePattern(logDir.getAbsolutePath() + "/bitherj.%d.log.gz"); rollingPolicy.setMaxHistory(7); rollingPolicy.start(); fileAppender.setEncoder(filePattern); fileAppender.setRollingPolicy(rollingPolicy); fileAppender.start(); final PatternLayoutEncoder logcatTagPattern = new PatternLayoutEncoder(); logcatTagPattern.setContext(context); logcatTagPattern.setPattern("%logger{0}"); logcatTagPattern.start(); final PatternLayoutEncoder logcatPattern = new PatternLayoutEncoder(); logcatPattern.setContext(context); logcatPattern.setPattern("[%thread] %msg%n"); logcatPattern.start(); final LogcatAppender logcatAppender = new LogcatAppender(); logcatAppender.setContext(context); logcatAppender.setTagEncoder(logcatTagPattern); logcatAppender.setEncoder(logcatPattern); logcatAppender.start(); final ch.qos.logback.classic.Logger log = context.getLogger(Logger.ROOT_LOGGER_NAME); log.addAppender(fileAppender); log.addAppender(logcatAppender); log.setLevel(Level.INFO); }
Example 20
Source File: LogbackUtil.java From summerframework with Apache License 2.0 | 2 votes |
public RollingFileAppender getAppender(String name, Level level, String fileName) { DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE); LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory(); RollingFileAppender appender = new RollingFileAppender(); LevelFilter levelFilter = getLevelFilter(level); levelFilter.start(); appender.addFilter(levelFilter); appender.setContext(context); appender.setName(name); appender.setFile(OptionHelper.substVars("/opt/app/logs/" + fileName + ".json", context)); appender.setAppend(true); appender.setPrudent(false); SizeAndTimeBasedRollingPolicy policy = new SizeAndTimeBasedRollingPolicy(); String fp = OptionHelper.substVars( "/opt/app/logs/histroy" + fileName + format.format(new Date()) + "/.%d{yyyy-MM-dd}.%i.json", context); policy.setMaxFileSize(FileSize.valueOf("128MB")); policy.setFileNamePattern(fp); policy.setMaxHistory(7); policy.setTotalSizeCap(FileSize.valueOf("32GB")); policy.setParent(appender); policy.setContext(context); policy.start(); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(context); encoder.setPattern("%d %p (%file:%line\\)- %m%n"); encoder.start(); appender.setRollingPolicy(policy); appender.setEncoder(encoder); appender.start(); return appender; }