/** * Create a Console Appender. * @param layout The layout to use (required). * @param filter The Filter or null. * @param t The target ("SYSTEM_OUT" or "SYSTEM_ERR"). The default is "SYSTEM_OUT". * @param follow If true will follow changes to the underlying output stream. * @param name The name of the Appender (required). * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise * they are propagated to the caller. * @return The ConsoleAppender. */ @PluginFactory public static ConsoleAppender createAppender( @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filters") final Filter filter, @PluginAttribute("target") final String t, @PluginAttribute("name") final String name, @PluginAttribute("follow") final String follow, @PluginAttribute("ignoreExceptions") final String ignore) { if (name == null) { LOGGER.error("No name provided for ConsoleAppender"); return null; } if (layout == null) { layout = PatternLayout.createLayout(null, null, null, null, null); } final boolean isFollow = Boolean.parseBoolean(follow); final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); final Target target = t == null ? Target.SYSTEM_OUT : Target.valueOf(t); return new ConsoleAppender(name, layout, filter, getManager(isFollow, target, layout), ignoreExceptions); }
/** * Factory method. Log4j will parse the configuration and call this factory * method to construct the appender with * the configured attributes. * * @param name Name of appender * @param layout Log layout of appender * @param filter Filter for appender * @return The TextAreaAppender */ @PluginFactory public static TextAreaAppender createAppender( @PluginAttribute("name") String name, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter) { if (name == null) { LOGGER.error("No name provided for TextAreaAppender"); return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } return new TextAreaAppender(name, filter, layout, true); }
public void watch(Class<?> loggerClass, Level level) { this.loggerClass = loggerClass; Appender appender = new AbstractAppender(APPENDER_NAME, null, PatternLayout.createDefaultLayout()) { @Override public void append(LogEvent event) { logEvents.add(event); } }; appender.start(); final LoggerContext ctx = getLoggerContext(); LoggerConfig loggerConfig = ctx.getConfiguration().getLoggerConfig(loggerClass.getName()); oldLevel = loggerConfig.getLevel(); loggerConfig.setLevel(level); loggerConfig.addAppender(appender, level, null); ctx.updateLoggers(); }
/** * Add an appender to Log4j which sends all INFO+ messages to a separate file which will be used * later to scan for suspect strings. The pattern of the messages conforms to the original log * format so that hydra will be able to parse them. */ private static void addSuspectFileAppender(final String workspaceDir) { final String suspectFilename = new File(workspaceDir, SUSPECT_FILENAME).getAbsolutePath(); final LoggerContext appenderContext = ((org.apache.logging.log4j.core.Logger) LogManager.getLogger(LogService.BASE_LOGGER_NAME)) .getContext(); final PatternLayout layout = PatternLayout.createLayout( "[%level{lowerCase=true} %date{yyyy/MM/dd HH:mm:ss.SSS z} <%thread> tid=%tid] %message%n%throwable%n", null, null, null, Charset.defaultCharset(), true, false, "", ""); final FileAppender fileAppender = FileAppender.createAppender(suspectFilename, "true", "false", DUnitLauncher.class.getName(), "true", "false", "false", "0", layout, null, null, null, appenderContext.getConfiguration()); fileAppender.start(); LoggerConfig loggerConfig = appenderContext.getConfiguration().getLoggerConfig(LogService.BASE_LOGGER_NAME); loggerConfig.addAppender(fileAppender, Level.INFO, null); }
@PluginFactory public static TerminalConsoleAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Filters") Filter filter, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginAttribute("ignoreExceptions") String ignore) { if (name == null) { LOGGER.error("No name provided for TerminalConsoleAppender"); return null; } if (layout == null) { layout = PatternLayout.createLayout(null, null, null, null, null); } boolean ignoreExceptions = parseBoolean(ignore, true); // This is handled by jline System.setProperty("log4j.skipJansi", "true"); return new TerminalConsoleAppender(name, filter, layout, ignoreExceptions); }
/** * Setup the file logger so that it outputs messages to the appropriately named file, * depending on the subtitle of the current program instance. */ private static void configureLoggers( String subtitle ) { // Remove all characters that are not allowed for Windows filenames. subtitle = subtitle.replaceAll( "[\\s" + Pattern.quote( "\\/:*?\"<>|" ) + "]", "" ); LoggerContext context = (LoggerContext)LogManager.getContext(); Configuration config = context.getConfiguration(); PatternLayout layout = PatternLayout.createLayout( "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%throwable%n", null, null, null, Charset.defaultCharset(), false, false, null, null ); FileAppender appender = FileAppender.createAppender( "logs/log-" + subtitle + ".txt", "false", "false", "LogFile-" + subtitle, "true", "true", "true", "8192", layout, null, "false", "", config ); org.apache.logging.log4j.core.Logger rootLogger = (org.apache.logging.log4j.core.Logger)LogManager.getRootLogger(); rootLogger.addAppender( appender ); appender.start(); }
public ProcessCallable(MCRSession session, Session webSocketSession) { this.commands = new ObservableCommandList(); this.session = session; this.webSocketSession = webSocketSession; this.logs = new ObservableLogEventDeque(); this.logGrabber = new Log4JGrabber(MCRWebCLIContainer.class.getSimpleName() + session.getID(), null, PatternLayout.createDefaultLayout()); this.logGrabber.start(); this.commandListObserver = new CommandListObserver(commands, webSocketSession); commands.addObserver(commandListObserver); this.logEventQueueObserver = new LogEventDequeObserver(logs, webSocketSession); logs.addObserver(logEventQueueObserver); this.currentCommand = ""; this.continueIfOneFails = false; }
private static void addFileAppender() throws IOException { final String tempDir = System.getProperty("java.io.tmpdir"); final File logFile = new File(tempDir, "meghanada_server.log"); final LoggerContext context = (LoggerContext) LogManager.getContext(false); final Configuration configuration = context.getConfiguration(); final LoggerConfig loggerConfig = configuration.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); final FileAppender fileAppender = FileAppender.newBuilder() .withName("file") .withLayout( PatternLayout.newBuilder() .withPattern("[%d][%-5.-5p][%-14.-14c{1}:%4L] %-22.-22M - %m%n") .build()) .withFileName(logFile.getCanonicalPath()) .build(); configuration.addAppender(fileAppender); loggerConfig.addAppender(fileAppender, Level.ERROR, null); context.updateLoggers(); }
public static PatternLayout createLayout(String logFormat) throws ReflectiveOperationException { try { Method builder = PatternLayout.class .getDeclaredMethod("createLayout", String.class, Configuration.class, RegexReplacement.class , String.class, String.class); return (PatternLayout) builder.invoke(null, logFormat, new DefaultConfiguration(), null , Charset.defaultCharset().name(), "true"); } catch (NoSuchMethodException methodEx) { return PatternLayout.newBuilder() .withCharset(Charset.defaultCharset()) .withPattern(logFormat) .withConfiguration(new DefaultConfiguration()) .withAlwaysWriteExceptions(true) .build(); } }
/** * Update the log configuration based on command line options * * @param cl */ protected final void updateLogConfig(CommandLine cl) { String logFile = cl.getOptionValue(OPT_LOG_FILE.getLongOpt(), null); String logLevel = cl.getOptionValue(OPT_LOG_LEVEL.getLongOpt(), null); if (logFile != null || logLevel != null) { logFile = logFile != null ? logFile : getDefaultLogFileName(); logLevel = logLevel != null ? logLevel : DEFAULT_LOG_LEVEL; LoggerContext context = (LoggerContext) LogManager.getContext(false); Configuration configuration = context.getConfiguration(); FileAppender appender = FileAppender.newBuilder() .withName("File") .withFileName(logFile) .withLayout(PatternLayout.newBuilder().withPattern(PatternLayout.SIMPLE_CONVERSION_PATTERN).build()) .withAppend(false) .build(); appender.start(); configuration.getRootLogger().addAppender(appender, Level.getLevel(logLevel), null); configuration.getRootLogger().setLevel(Level.getLevel(logLevel)); context.updateLoggers(); } }
/** * Creates a new instance of the class. Required by Log4J2. * * @param config the configuration * @param options the options * @return a new instance, or {@code null} if the options are invalid */ public static ColorConverter newInstance(Configuration config, String[] options) { if (options.length < 1) { LOGGER.error("Incorrect number of options on style. " + "Expected at least 1, received {}", options.length); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on style"); return null; } PatternParser parser = PatternLayout.createPatternParser(config); List<PatternFormatter> formatters = parser.parse(options[0]); AnsiElement element = (options.length == 1 ? null : ELEMENTS.get(options[1])); return new ColorConverter(formatters, element); }
protected void configureLogging() { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Configuration config = ctx.getConfiguration(); Layout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, null, config, null, null, true, false, null, null); Appender appender = FileAppender.createAppender(workDir + "/logs/camel-standalone.log", "false", "false", "File", "true", "false", "false", "4000", layout, null, "false", null, config); appender.start(); config.addAppender(appender); AppenderRef ref = AppenderRef.createAppenderRef("File", null, null); AppenderRef[] refs = new AppenderRef[] {ref}; LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "StandaloneFileLoggerConfig", "true", refs, null, config, null ); loggerConfig.addAppender(appender, null, null); config.addLogger("StandaloneFileLoggerConfig", loggerConfig); ctx.updateLoggers(); }
protected SimpleJSONLayout(final Configuration config, final boolean locationInfo, final boolean properties, final boolean complete, final boolean eventEol, final String headerPattern, final String footerPattern, final Charset charset, final KeyValuePair[] additionalFields) { super(config, charset, // PatternLayout.newSerializerBuilder() // .setConfiguration(config).setReplace(null).setPattern(headerPattern) // .setDefaultPattern(DEFAULT_HEADER) // .setPatternSelector(null).setAlwaysWriteExceptions(false).setNoConsoleNoAnsi(false) // .build(), // PatternLayout.newSerializerBuilder() // .setConfiguration(config).setReplace(null).setPattern(footerPattern) // .setDefaultPattern(DEFAULT_FOOTER) // .setPatternSelector(null).setAlwaysWriteExceptions(false).setNoConsoleNoAnsi(false) // .build()); this.locationInfo = locationInfo; this.properties = properties; this.complete = complete; this.additionalFields = additionalFields; this.layoutStartTime = System.currentTimeMillis(); this.layoutSequence = new AtomicLong(); this.interpolator = new Interpolator(new MapLookup(getConfiguration().getProperties()), getConfiguration().getPluginPackages()); this.eol = !eventEol ? COMPACT_EOL : DEFAULT_EOL; }
@PluginFactory public static ConsoleLogAppender createAppender(@PluginAttribute("name") final String name, @PluginAttribute("ignoreExceptions") final String ignore, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filters") final Filter filter, @PluginAttribute("target") String target) { final boolean ignoreExceptions = Boolean.parseBoolean(ignore); if (name == null) { ConsoleLogAppender.LOGGER.error("No name provided for ConsoleLogAppender"); return null; } if (target == null) { target = name; } ConsoleLogAppender.QUEUE_LOCK.writeLock().lock(); BlockingQueue<String> queue = ConsoleLogAppender.QUEUES.get(target); if (queue == null) { queue = new LinkedBlockingQueue<>(); ConsoleLogAppender.QUEUES.put(target, queue); } ConsoleLogAppender.QUEUE_LOCK.writeLock().unlock(); if (layout == null) { layout = PatternLayout.createLayout(null, null, null, null, true, !Nukkit.useConsole, null, null); } return new ConsoleLogAppender(name, filter, layout, ignoreExceptions, queue); }
@PluginFactory public static WorkspaceLogAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter) { if (name == null) { LOGGER.error("No name provided for WorkspaceLogAppender"); return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } return new WorkspaceLogAppender(name, filter, layout, true); }
@PluginFactory public static SlackAppender createAppender( @PluginAttribute("name") String name, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter, @PluginAttribute("webhookUrl") URL webhookUrl, @PluginAttribute("channel") String channel, @PluginAttribute(value = "username", defaultString = "Blazkowicz") String username, @PluginAttribute(value = "meltdownProtection", defaultBoolean = true) boolean meltdownProtection, @PluginAttribute(value = "similarMessageSize", defaultInt = 50) int similarMessageSize, @PluginAttribute(value = "timeBetweenSimilarLogsMs", defaultInt = 60000) int timeBetweenSimilarLogsMs, @PluginAttribute(value = "packagesToMute", defaultString = "") String packagesToMute, @PluginAttribute(value = "httpClientImpl", defaultString = "") String httpClientImpl) { if (name == null) { LOGGER.error("No name provided for MyCustomAppenderImpl"); return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } Client client = findClientImpl(httpClientImpl); SlackAppender slackAppender = new SlackAppender(name, filter, layout, webhookUrl, username, channel, meltdownProtection, similarMessageSize, timeBetweenSimilarLogsMs, client); slackAppender.setPackagesToMute(packagesToMute); return slackAppender; }
@PluginFactory public static DebuggerAppender createAppender(@PluginAttribute("name") String name, @PluginElement("Layout") Layout<?> layout, @PluginElement("Filters") Filter filter, @PluginAttribute("ignoreExceptions") boolean ignoreExceptions) { if (name == null) { LOGGER.error("No name provided for JTextAreaAppender"); return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } return new DebuggerAppender(name, layout, filter, ignoreExceptions); }
private static Logger configureLog4j() { LoggerContext context = (LoggerContext) LogManager.getContext(); Configuration config = context.getConfiguration(); PatternLayout layout = PatternLayout.createLayout("%m%n", null, null, Charset.defaultCharset(), false, false, null, null); Appender appender = ConsoleAppender.createAppender(layout, null, null, "CONSOLE_APPENDER", null, null); appender.start(); AppenderRef ref = AppenderRef.createAppenderRef("CONSOLE_APPENDER", null, null); AppenderRef[] refs = new AppenderRef[]{ref}; LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, "CONSOLE_LOGGER", "com", refs, null, null, null); loggerConfig.addAppender(appender, null, null); config.addAppender(appender); config.addLogger("Main.class", loggerConfig); context.updateLoggers(config); return LogManager.getContext().getLogger("Main.class"); }
@PluginFactory public static ApiLogAppenderImpl createAppender( @PluginAttribute("name") String name, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter, @PluginAttribute("format") String otherAttribute) { if (name == null) { LOGGER.error("No name provided for ApiLogAppenderImpl"); return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } return new ApiLogAppenderImpl(name, filter, layout, true); }
/** * A custom appender needs to declare a factory method annotated with `@PluginFactory`. * Log4j will parse the configuration and call this factory method to construct an appender instance with * the configured attributes. * * @param name - the logger name * @param layout - the layout * @param filter - the filter * @param otherAttribute - other attributes * @return a text area logger */ @PluginFactory public static TextAreaLogger createAppender( @PluginAttribute("name") String name, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter, @PluginAttribute("otherAttribute") String otherAttribute) { if (name == null) { LOGGER.error("No name provided for MyCustomAppenderImpl"); return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } return new TextAreaLogger(name, filter, layout, true); }
@PluginFactory public static MongoFXMessageAppender createAppender(@PluginAttribute("name") String name, @PluginAttribute("ignoreExceptions") boolean ignoreExceptions, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filters") Filter filter) { if (name == null) { LOGGER.error("No name provided for StubAppender"); return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } return new MongoFXMessageAppender(name, filter, layout, ignoreExceptions); }
public static void main(String[] args) throws UnRetriableException{ LoggerContext ctx = (LoggerContext) LogManager.getContext(false); AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout()); appender.start(); config.addAppender(appender); AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) }; LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.INFO, LogManager.ROOT_LOGGER_NAME, "true", refs, null, config, null); loggerConfig.addAppender(appender, null, null); config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig); ctx.updateLoggers(); Runner runner = defaultRunner(); runner.init(); runner.start(); }
public static void main(String[] args) throws UnRetriableException { LoggerContext ctx = (LoggerContext) LogManager.getContext(false); AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration(); ConsoleAppender appender = ConsoleAppender.createDefaultAppenderForLayout(PatternLayout.createDefaultLayout()); appender.start(); config.addAppender(appender); AppenderRef[] refs = new AppenderRef[] { AppenderRef.createAppenderRef(appender.getName(), null, null) }; LoggerConfig loggerConfig = LoggerConfig.createLogger("false", Level.WARN, LogManager.ROOT_LOGGER_NAME, "true", refs, null, config, null); loggerConfig.addAppender(appender, null, null); config.addLogger(LogManager.ROOT_LOGGER_NAME, loggerConfig); ctx.updateLoggers(); Runner runner = defaultRunner(); runner.init(); runner.start(); }
@Before public void setUpAppender() throws IOException { file = folder.newFile(); final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); final Configuration config = ctx.getConfiguration(); final LoggerConfig root = ((AbstractConfiguration) config).getRootLogger(); PatternLayout layout = PatternLayout.createLayout("[%p] %m%n", config, null, null, true, false, null, null); FileAppender appender = FileAppender.createAppender(file.getAbsolutePath(), "true", "false", "TestLogFile", "true", "false", "false", "8192", layout, null, "false", null, config); appender.start(); config.addAppender(appender); root.addAppender(appender, null, null); ctx.updateLoggers(); }
@PluginFactory public static MemoryAppender createAppender( @PluginAttribute("name") String name, @PluginElement("Layout") Layout<? extends Serializable> layout, @PluginElement("Filter") final Filter filter, @PluginAttribute("numberOfLines") String numberOfLines) { if (name == null) { LOGGER.error("No name provided for MemoryAppender"); return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } if (numberOfLines != null) { logsize = Integer.valueOf(numberOfLines); } return new MemoryAppender(name, filter, layout, true); }
@PluginFactory public static ConsoleWindowAppender createAppender(@PluginAttribute("name") String name, @PluginAttribute("ignoreExceptions") boolean ignoreExceptions, @PluginElement("Layout") Layout layout, @PluginElement("Filters") Filter filter) { if (name == null) { LOGGER.error("No name provided for StubAppender"); return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } //noinspection unchecked return new ConsoleWindowAppender(name, filter, layout, ignoreExceptions); }
@PluginFactory public static JesterJAppender createAppender(@PluginAttribute("name") String name, @PluginAttribute("ignoreExceptions") boolean ignoreExceptions, @PluginElement("Layout") Layout layout, @PluginElement("Filters") Filter filter) { if (name == null) { LOGGER.error("No name provided for JesterJAppender"); return null; } manager = createManager(); if (manager == null) { return null; } if (layout == null) { layout = PatternLayout.createDefaultLayout(); } cassandra.addStatement(FTI_INSERT_Q, INSERT_FTI); cassandra.addStatement(REG_INSERT_Q, INSERT_REG); return new JesterJAppender(name, layout, filter, manager, ignoreExceptions); }