private Appender<ILoggingEvent> fileAppender(LogbackConfigurator config, String logFile) { RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<ILoggingEvent>(); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); String logPattern = this.patterns.getProperty("file", FILE_LOG_PATTERN); encoder.setPattern(OptionHelper.substVars(logPattern, config.getContext())); appender.setEncoder(encoder); config.start(encoder); appender.setFile(logFile); FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy(); rollingPolicy.setFileNamePattern(logFile + ".%i"); appender.setRollingPolicy(rollingPolicy); rollingPolicy.setParent(appender); config.start(rollingPolicy); SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>(); triggeringPolicy.setMaxFileSize("10MB"); appender.setTriggeringPolicy(triggeringPolicy); config.start(triggeringPolicy); config.appender("FILE", appender); return appender; }
public WebSocketLogAppender(String sessionId) { // deliberately NOT the com.intuit form else will pick up all those // this is supposed to isolate user-session s this.sessionId = sessionId; logger = (Logger) LoggerFactory.getLogger(sessionId); sb = new StringBuilder(); setName("karate-web"); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); setContext(lc); encoder = new PatternLayoutEncoder(); encoder.setPattern("%d{HH:mm:ss.SSS} %-5level - %msg%n"); encoder.setContext(context); encoder.start(); start(); logger.addAppender(this); logger.setLevel(Level.DEBUG); }
private static void configureInMemoryLogging() { org.slf4j.Logger rootLogger = LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME); Logger logbackRootLogger = (Logger) rootLogger; logbackRootLogger.setLevel(Level.INFO); OutputStreamAppender<ILoggingEvent> appender = new OutputStreamAppender<>(); LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); PatternLayoutEncoder ple = new PatternLayoutEncoder(); ple.setPattern("[%date] [%level] [%logger{10}] %msg%n"); ple.setContext(lc); ple.start(); try { ple.init(bos); } catch (IOException e) { } appender.setContext(lc); appender.setOutputStream(bos); appender.setName("buffered"); appender.setEncoder(ple); appender.start(); logbackRootLogger.detachAppender("console"); logbackRootLogger.addAppender(appender); logbackRootLogger.setAdditive(true); }
@Test public void log() throws Exception { Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); LoggerContext context = logger.getLoggerContext(); context.reset(); Map<String, String> ruleRegistry = (Map) context.getObject(CoreConstants.PATTERN_RULE_REGISTRY); if (ruleRegistry == null) { ruleRegistry = new HashMap<String, String>(); context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry); } ruleRegistry.put("ex1L", OnelineThrowableProxyConverter.class.getCanonicalName()); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(context); encoder.setPattern("%d{yyyy/MM/dd HH:mm:ss:SSS}\\t%-5level\\t%msg\\t%ex1L"); encoder.start(); ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>(); appender.setContext(context); appender.setEncoder(encoder); appender.start(); logger.addAppender(appender); logger.error("error", new RuntimeException("foo")); }
@Test public void log() throws Exception { Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); LoggerContext context = logger.getLoggerContext(); context.reset(); Map<String, String> ruleRegistry = (Map) context.getObject(CoreConstants.PATTERN_RULE_REGISTRY); if (ruleRegistry == null) { ruleRegistry = new HashMap<String, String>(); context.putObject(CoreConstants.PATTERN_RULE_REGISTRY, ruleRegistry); } ruleRegistry.put("xEx1L", OnelineExtendedThrowableProxyConverter.class.getCanonicalName()); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(context); encoder.setPattern("%d{yyyy/MM/dd HH:mm:ss:SSS}\\t%-5level\\t%msg\\t%xEx1L"); encoder.start(); ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>(); appender.setContext(context); appender.setEncoder(encoder); appender.start(); logger.addAppender(appender); logger.error("error", new RuntimeException()); }
/** * 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); }
/** * 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); System.out.println("LogUtil. Logging to " + logPath); }
public FileAppender<ILoggingEvent> buildFileAppender(LoggerContext context, String file) { FileAppender<ILoggingEvent> fileAppender = new FileAppender<>(); fileAppender.setFile(file); fileAppender.setContext(context); fileAppender.setPrudent(true); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(context); encoder.setPattern(primaryConfiguration.getLoggingPattern().or(baseConfiguration.getLoggingPattern()).or(JavaUtils.LOGBACK_LOGGING_PATTERN)); encoder.start(); fileAppender.setEncoder(encoder); fileAppender.start(); return fileAppender; }
static void logToFile(Parameters parameters) { 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.setFile(parameters.getWorkingDirectory() + "/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); }
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); }
@Override public Appender<ILoggingEvent> build(LoggerContext context, String s, Layout<ILoggingEvent> layout) { Preconditions.checkNotNull(context); WiretapAppender appender = new WiretapAppender(); final PatternLayoutEncoder patternEncoder = new PatternLayoutEncoder(); patternEncoder.setContext(context); patternEncoder.setPattern("%-5p [%thread] [%d] %c: %m%n%rEx"); patternEncoder.start(); appender.setContext(context); appender.setName("wiretap-appender"); appender.setEncoder(patternEncoder); addThresholdFilter(appender, threshold); appender.start(); return wrapAsync(appender); }
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(); }
/** * Initializes the logger and the requested log level. * * @param level Logging level threshold */ @SuppressWarnings("unchecked") public static void configure(final Level level) { final Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); root.setLevel(level); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(root.getLoggerContext()); encoder.setPattern("%msg%n"); encoder.start(); Iterator<Appender<ILoggingEvent>> iterator = root.iteratorForAppenders(); while (iterator.hasNext()) { Appender<ILoggingEvent> appender = iterator.next(); if (appender instanceof OutputStreamAppender) { ((OutputStreamAppender) appender).setEncoder(encoder); } appender.stop(); appender.start(); } }
private void setupConsole(LogFixtureBuilder builder) { drillLogger = (Logger)LoggerFactory.getLogger(DRILL_PACKAGE_NAME); if (drillLogger.getAppender("STDOUT") != null) { return; } LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); ple = new PatternLayoutEncoder(); ple.setPattern(builder.consoleFormat); ple.setContext(lc); ple.start(); appender = new ConsoleAppender<>( ); appender.setContext(lc); appender.setName("Console"); appender.setEncoder( ple ); appender.start(); Logger root = (Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); root.addAppender(appender); drillLogger.addAppender(appender); }
private Logger createLogger(File logFile) { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); ch.qos.logback.classic.Logger log = loggers.get(); log.setAdditive(false); FileAppender fileAppender = new FileAppender(); fileAppender.setContext(context); fileAppender.setName(Thread.currentThread().getName()); fileAppender.setFile(logFile.getAbsolutePath()); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(context); encoder.setPattern(LOG_PATTERN); encoder.start(); fileAppender.setEncoder(encoder); fileAppender.start(); log.detachAndStopAllAppenders(); log.addAppender(fileAppender); return log; }
private void transformDateFormat() { if (!(encoder instanceof PatternLayoutEncoder)) { return; } String encoderPattern = ((PatternLayoutEncoder) encoder).getPattern(); int startPos = encoderPattern.indexOf("%d{"); // not found date pattern if (startPos == -1) { return; } int endPos = encoderPattern.indexOf("}", startPos); String pattern = encoderPattern.substring(startPos + 3, endPos); // date pattern is empty if (pattern.trim().length() == 0) { return; } dateFormat = new SimpleDateFormat(pattern); }
/** * Creates a new logger. * * @param name a name for the logger * @param level the logging level for the logger * @param file the file to output all logging to * @return the created logger */ public static Logger createLogger(String name, Level level, String file) { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); PatternLayoutEncoder ple = new PatternLayoutEncoder(); ple.setPattern("%date %level [%thread] %logger{10} [%file:%line]%n%msg%n%n"); ple.setContext(lc); ple.start(); FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>(); fileAppender.setFile(file); fileAppender.setEncoder(ple); fileAppender.setAppend(false); fileAppender.setContext(lc); fileAppender.start(); logger = (Logger) LoggerFactory.getLogger(name); logger.detachAndStopAllAppenders(); // detach console (doesn't seem to work) logger.addAppender(fileAppender); // attach file appender logger.setLevel(level); logger.setAdditive(true); // set to true if root should log too return logger; }
public static void configure(LoggerContext lc) { StatusManager sm = lc.getStatusManager(); if(sm != null) { sm.add(new InfoStatus("Setting up default configuration.", lc)); } ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>(); 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(); Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.addAppender(ca); }
static LoggerContext buildLoggerContext(String stamp, String filename, boolean safetyMode) { LoggerContext loggerContext = new LoggerContext(); FileAppender<ILoggingEvent> fa = new FileAppender<ILoggingEvent>(); PatternLayoutEncoder patternLayout = new PatternLayoutEncoder(); patternLayout.setPattern(stamp + " %5p - %m%n"); patternLayout.setContext(loggerContext); patternLayout.start(); fa.setEncoder(patternLayout); fa.setFile(filename); fa.setAppend(true); fa.setPrudent(safetyMode); fa.setContext(loggerContext); fa.start(); ch.qos.logback.classic.Logger root = loggerContext .getLogger(Logger.ROOT_LOGGER_NAME); root.addAppender(fa); return loggerContext; }
static LoggerContext buildLoggerContext(String filename, boolean safetyMode) { LoggerContext loggerContext = new LoggerContext(); FileAppender<ILoggingEvent> fa = new FileAppender<ILoggingEvent>(); PatternLayoutEncoder patternLayout = new PatternLayoutEncoder(); patternLayout.setPattern("%5p %c - %m%n"); patternLayout.setContext(loggerContext); patternLayout.start(); fa.setEncoder(patternLayout); fa.setFile(filename); fa.setAppend(false); fa.setPrudent(safetyMode); fa.setContext(loggerContext); fa.start(); ch.qos.logback.classic.Logger root = loggerContext .getLogger(Logger.ROOT_LOGGER_NAME); root.addAppender(fa); return loggerContext; }
static Logger buildLoggerContext(LoggerContext lc) { Logger root = lc.getLogger(Logger.ROOT_LOGGER_NAME); PatternLayoutEncoder patternLayout = new PatternLayoutEncoder(); patternLayout.setContext(lc); patternLayout.setPattern("%d %l [%t] - %msg%n"); patternLayout.start(); FileAppender<ILoggingEvent> fileAppender = new FileAppender<ILoggingEvent>(); fileAppender.setContext(lc); fileAppender.setFile("target/lbclassic135.log"); fileAppender.setEncoder(patternLayout); fileAppender.setAppend(false); fileAppender.start(); root.addAppender(fileAppender); return lc.getLogger(LoggingToFileThroughput.class); }
static public void main(String[] args) throws Exception { Logger rootLogger = (Logger) LoggerFactory.getLogger("root"); LoggerContext loggerContext = rootLogger.getLoggerContext(); loggerContext.reset(); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern("%-5level [%thread]: %message%n"); encoder.start(); ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>(); appender.setContext(loggerContext); appender.setEncoder(encoder); appender.start(); rootLogger.addAppender(appender); rootLogger.debug("Message 1"); rootLogger.warn("Message 2"); }
public IOPerformance(boolean _immediateFlush, long _len) { this.len = _len; this.immediateFlush = _immediateFlush; context = new LoggerContext(); logger = context.getLogger("logger-" + getName()); // A FileAppender is created according to the buffering and // immediate flush setting of this IO instance. FileAppender<ILoggingEvent> fa = new FileAppender<ILoggingEvent>(); fa.setName("FILE"); PatternLayoutEncoder pa = new PatternLayoutEncoder(); pa.setPattern("%r %5p %c [%t] - %m%n"); pa.setContext(context); pa.start(); fa.setEncoder(pa); fa.setFile(LOG_FILE); fa.setAppend(true); fa.setContext(context); fa.start(); ((ch.qos.logback.classic.Logger) logger).addAppender(fa); StatusPrinter.print(context); }
static void programmaticConfiguration() { // Configure logback LoggerContext loggerContext = (LoggerContext) LoggerFactory .getILoggerFactory(); loggerContext.reset(); PatternLayoutEncoder layout = new PatternLayoutEncoder(); layout.setContext(loggerContext); layout.setPattern("%X{first} %X{last} - %m%n"); layout.start(); ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<ILoggingEvent>(); appender.setContext(loggerContext); appender.setEncoder(layout); appender.start(); // cast root logger to c.q.logback.classic.Logger so that we can attach // an appender to it ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) LoggerFactory .getLogger("root"); root.addAppender(appender); }
/** {@inheritDoc} */ @Override public void configure(LoggerContext loggerContext) { StatusManager sm = loggerContext.getStatusManager(); if (sm != null) { sm.add(new InfoStatus("Setting up GreenPepper PHP Runner configuration.", loggerContext)); } ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>(); ca.setContext(loggerContext); ca.setName("console"); PatternLayoutEncoder pl = new PatternLayoutEncoder(); pl.setContext(loggerContext); pl.setPattern("%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %-36logger{36} - %msg%n"); pl.start(); ca.setEncoder(pl); ca.start(); Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.addAppender(ca); rootLogger.setLevel(Level.INFO); }
/** {@inheritDoc} */ @Override public void configure(LoggerContext loggerContext) { StatusManager sm = loggerContext.getStatusManager(); if (sm != null) { sm.add(new InfoStatus("Setting up default configuration.", loggerContext)); } ConsoleAppender<ILoggingEvent> ca = new ConsoleAppender<ILoggingEvent>(); ca.setContext(loggerContext); ca.setName("console"); PatternLayoutEncoder pl = new PatternLayoutEncoder(); pl.setContext(loggerContext); pl.setPattern("[%-5level] %msg%n"); pl.start(); ca.setEncoder(pl); ca.start(); Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); rootLogger.addAppender(ca); rootLogger.setLevel(Level.WARN); loggerContext.getLogger("com.greenpepper.maven.plugin").setLevel(Level.INFO); }
/** * Test abstract functions (using console implementation) and the functions * of console * */ @Test public void testAbstractAndConsole() { BaleenConsoleLoggerBuilder builder = new BaleenConsoleLoggerBuilder(NAME, BaleenLogging.DEFAULT_PATTERN, new MinMaxFilter(Level.INFO, Level.WARN)); LoggerContext context = new LoggerContext(); Encoder<ILoggingEvent> encoder = new PatternLayoutEncoder(); Appender<ILoggingEvent> appender = builder.build(context, encoder); assertEquals(NAME, builder.getName()); assertTrue(appender instanceof ConsoleAppender); assertEquals(encoder, ((ConsoleAppender<ILoggingEvent>) appender).getEncoder()); assertNotNull(appender); assertEquals(NAME, appender.getName()); Filter<ILoggingEvent> filter = appender.getCopyOfAttachedFiltersList().get(0); assertTrue(filter instanceof MinMaxFilter); assertEquals(Level.INFO, ((MinMaxFilter) filter).getMin()); assertEquals(Level.WARN, ((MinMaxFilter) filter).getMax()); }
private void updateConsole(String changedProp) { LoggerContext loggerContext = (LoggerContext) getContext(); Logger logbackLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME); ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) logbackLogger .getAppender(APPENDER_CONSOLE); if (config.getBoolean(CONSOLE_ENABLED, true)) { logbackLogger.detachAppender(consoleAppender); consoleAppender = new ConsoleAppender<>(); consoleAppender.setContext(loggerContext); consoleAppender.setName(APPENDER_CONSOLE); PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(loggerContext); encoder.setPattern(config.getString(CONSOLE_PATTERN, DEFAULT_PATTERN)); consoleAppender.setEncoder(encoder); encoder.start(); consoleAppender.start(); logbackLogger.addAppender(consoleAppender); } else { logbackLogger.detachAppender(consoleAppender); } }
public Logger getLogger() { LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory(); Logger rootLogger = context.getLogger("ROOT"); rootLogger.detachAppender("console"); // Just a simple pattern for outputting the messages, includes the ISO8601 formatted timestamp and marker. PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(context); encoder.setPattern("%msg"); encoder.start(); RollingFileAppender appender = new RollingFileAppender<>(); appender.setContext(context); appender.setRollingPolicy(new ExternalRotateRollingPolicy()); appender.getRollingPolicy().setParent(appender); appender.setFile(new File(folder.getRoot(), "log").getAbsolutePath()); appender.setEncoder(encoder); appender.start(); rootLogger.addAppender(appender); context.start(); return rootLogger; }
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; }
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; }
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; }
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); }
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; }
private Appender buildDailyMaxRollingAppender(String name, String fileName, String pattern, int maxBackupIndex) { RollingFileAppender appender = new RollingFileAppender(); appender.setName(name); appender.setAppend(true); appender.setFile(new File(getLogPath(), fileName).getAbsolutePath()); TimeBasedRollingPolicy rolling = new TimeBasedRollingPolicy(); rolling.setFileNamePattern(new File(getLogPath(), fileName).getAbsolutePath() + ".%d{yyyy-MM-dd-HH}"); rolling.setMaxHistory(maxBackupIndex); rolling.setParent(appender); appender.setRollingPolicy(rolling); PatternLayoutEncoder layout = new PatternLayoutEncoder(); layout.setPattern(pattern); layout.setCharset(Charset.forName(getEncoding())); appender.setEncoder(layout); // 启动 appender.start(); return appender; }
@Override public Appender<ILoggingEvent> buildAppender(Context logcingContext, String fileName) throws JoranException { // first sets up the paterns for log layout PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setPattern(logTemplate); encoder.setContext(logcingContext); encoder.start(); // then create a file appender for the given filename FileAppender<ILoggingEvent> appender = new FileAppender<ILoggingEvent>(); appender.setContext(logcingContext); appender.setAppend(false); appender.setFile(logFolderName + "/" + fileName + ".log"); appender.setEncoder(encoder); appender.start(); return appender; }
/** * Finds or creates a logger for the test case called tcName with an appender writing * to file named <tcName>.log * * @param tcName * @return */ private Logger getTestCaseLogger(String tcName, String sutName, String sutDir, String testScheduleName) { FileAppender<ILoggingEvent> fileAppender = appenderMap.get(tcName); if (fileAppender == null) { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); PatternLayoutEncoder ple = new PatternLayoutEncoder(); ple.setPattern("%date %level [%logger{36}] [%file:%line] %X{testcase}: %msg%n"); ple.setContext(lc); ple.start(); fileAppender = new FileAppender<ILoggingEvent>(); fileAppender.setFile(sutDir + '/' + testScheduleName + '/' + tcName + ".log"); fileAppender.setEncoder(ple); fileAppender.setContext(lc); fileAppender.start(); appenderMap.put(tcName, fileAppender); } Logger logger = (Logger) LoggerFactory.getLogger(tcName); logger.addAppender(fileAppender); return logger; }
private TmmUILogCollector() { Logger rootLogger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME); // create a new TmmUILogAppender - so we need not to put it in the logback.xml: PatternLayoutEncoder encoder = new PatternLayoutEncoder(); encoder.setContext(rootLogger.getLoggerContext()); encoder.setPattern("%d{HH:mm:ss.SSS} %-5level %logger{60} - %msg%n"); encoder.start(); logAppender = new TmmUILogAppender(); logAppender.setContext(rootLogger.getLoggerContext()); logAppender.setEncoder(encoder); logAppender.start(); rootLogger.addAppender(logAppender); }