Search in sources :

Example 61 with PatternLayout

use of org.apache.log4j.PatternLayout in project BiglyBT by BiglySoftware.

the class Log method execute.

@Override
public void execute(String commandName, final ConsoleInput ci, CommandLine commandLine) {
    Appender con = Logger.getRootLogger().getAppender("ConsoleAppender");
    List args = commandLine.getArgList();
    if ((con != null) && (!args.isEmpty())) {
        String subcommand = (String) args.get(0);
        if ("off".equalsIgnoreCase(subcommand)) {
            if (args.size() == 1) {
                con.addFilter(new DenyAllFilter());
                ci.out.println("> Console logging off");
            } else {
                String name = (String) args.get(1);
                Object[] entry = (Object[]) channel_listener_map.remove(name);
                if (entry == null) {
                    ci.out.println("> Channel '" + name + "' not being logged");
                } else {
                    ((LoggerChannel) entry[0]).removeListener((LoggerChannelListener) entry[1]);
                    ci.out.println("> Channel '" + name + "' logging off");
                }
            }
        } else if ("on".equalsIgnoreCase(subcommand)) {
            if (args.size() == 1) {
                if (commandLine.hasOption('f')) {
                    // send log output to a file
                    String filename = commandLine.getOptionValue('f');
                    try {
                        Appender newAppender = new FileAppender(new PatternLayout("%d{ISO8601} %c{1}-%p: %m%n"), filename, true);
                        newAppender.setName("ConsoleAppender");
                        Logger.getRootLogger().removeAppender(con);
                        Logger.getRootLogger().addAppender(newAppender);
                        ci.out.println("> Logging to filename: " + filename);
                    } catch (IOException e) {
                        ci.out.println("> Unable to log to file: " + filename + ": " + e);
                    }
                } else {
                    if (!(con instanceof ConsoleAppender)) {
                        Logger.getRootLogger().removeAppender(con);
                        con = new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
                        con.setName("ConsoleAppender");
                        Logger.getRootLogger().addAppender(con);
                    }
                    // switch back to console appender
                    ci.out.println("> Console logging on");
                }
                con.clearFilters();
            } else {
                // hack - dunno how to do plugin-specific logging using these damn appenders..
                Map channel_map = getChannelMap(ci);
                final String name = (String) args.get(1);
                LoggerChannel channel = (LoggerChannel) channel_map.get(name);
                if (channel == null) {
                    ci.out.println("> Channel '" + name + "' not found");
                } else if (channel_listener_map.get(name) != null) {
                    ci.out.println("> Channel '" + name + "' already being logged");
                } else {
                    LoggerChannelListener l = new LoggerChannelListener() {

                        @Override
                        public void messageLogged(int type, String content) {
                            ci.out.println("[" + name + "] " + content);
                        }

                        @Override
                        public void messageLogged(String str, Throwable error) {
                            ci.out.println("[" + name + "] " + str);
                            error.printStackTrace(ci.out);
                        }
                    };
                    channel.addListener(l);
                    channel_listener_map.put(name, new Object[] { channel, l });
                    ci.out.println("> Channel '" + name + "' on");
                }
            }
        } else if (subcommand.equalsIgnoreCase("list")) {
            Map channel_map = getChannelMap(ci);
            Iterator it = channel_map.keySet().iterator();
            while (it.hasNext()) {
                String name = (String) it.next();
                ci.out.println("  " + name + " [" + (channel_listener_map.get(name) == null ? "off" : "on") + "]");
            }
        } else {
            ci.out.println("> Command 'log': Subcommand '" + subcommand + "' unknown.");
        }
    } else {
        ci.out.println("> Console logger not found or missing subcommand for 'log'\r\n> log syntax: log [-f filename] (on [name]|off [name]|list)");
    }
}
Also used : Appender(org.apache.log4j.Appender) ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) DenyAllFilter(org.apache.log4j.varia.DenyAllFilter) FileAppender(org.apache.log4j.FileAppender) ConsoleAppender(org.apache.log4j.ConsoleAppender) LoggerChannelListener(com.biglybt.pif.logging.LoggerChannelListener) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) LoggerChannel(com.biglybt.pif.logging.LoggerChannel)

Example 62 with PatternLayout

use of org.apache.log4j.PatternLayout in project tez by apache.

the class Utils method setupRootLogger.

public static void setupRootLogger() {
    if (Strings.isNullOrEmpty(System.getProperty(LOG4J_CONFIGURATION))) {
        // By default print to console with INFO level
        Logger.getRootLogger().addAppender(new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
        Logger.getRootLogger().setLevel(Level.INFO);
    }
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) PatternLayout(org.apache.log4j.PatternLayout)

Example 63 with PatternLayout

use of org.apache.log4j.PatternLayout in project incubator-rya by apache.

the class DemoDriver method setupLogging.

private static void setupLogging() {
    // Turn off all the loggers and customize how they write to the console.
    final Logger rootLogger = LogManager.getRootLogger();
    rootLogger.setLevel(Level.OFF);
    final ConsoleAppender ca = (ConsoleAppender) rootLogger.getAppender("stdout");
    ca.setLayout(new PatternLayout("%-5p - %m%n"));
    // Turn the loggers used by the demo back on.
    log.setLevel(Level.INFO);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) PatternLayout(org.apache.log4j.PatternLayout) Logger(org.apache.log4j.Logger)

Example 64 with PatternLayout

use of org.apache.log4j.PatternLayout in project incubator-rya by apache.

the class RyaClientExample method setupLogging.

private static void setupLogging() {
    // Turn off all the loggers and customize how they write to the console.
    final Logger rootLogger = LogManager.getRootLogger();
    rootLogger.setLevel(Level.OFF);
    final ConsoleAppender ca = (ConsoleAppender) rootLogger.getAppender("stdout");
    ca.setLayout(new PatternLayout("%-5p - %m%n"));
    // Turn the logger used by the demo back on.
    log.setLevel(Level.INFO);
    Logger.getLogger(ClientCnxn.class).setLevel(Level.ERROR);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) PatternLayout(org.apache.log4j.PatternLayout) Logger(org.apache.log4j.Logger) ClientCnxn(org.apache.zookeeper.ClientCnxn)

Example 65 with PatternLayout

use of org.apache.log4j.PatternLayout in project incubator-rya by apache.

the class ProspectorExample method setupLogging.

private static void setupLogging() {
    // Turn off all the loggers and customize how they write to the console.
    final Logger rootLogger = LogManager.getRootLogger();
    rootLogger.setLevel(Level.OFF);
    final ConsoleAppender ca = (ConsoleAppender) rootLogger.getAppender("stdout");
    ca.setLayout(new PatternLayout("%-5p - %m%n"));
    // Turn the logger used by the demo back on.
    log.setLevel(Level.INFO);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) PatternLayout(org.apache.log4j.PatternLayout) Logger(org.apache.log4j.Logger)

Aggregations

PatternLayout (org.apache.log4j.PatternLayout)101 ConsoleAppender (org.apache.log4j.ConsoleAppender)51 Logger (org.apache.log4j.Logger)27 IOException (java.io.IOException)21 Appender (org.apache.log4j.Appender)18 FileAppender (org.apache.log4j.FileAppender)17 Layout (org.apache.log4j.Layout)15 File (java.io.File)14 RollingFileAppender (org.apache.log4j.RollingFileAppender)12 Test (org.junit.Test)8 CommandLineParser (org.apache.commons.cli.CommandLineParser)7 GnuParser (org.apache.commons.cli.GnuParser)7 ParseException (org.apache.commons.cli.ParseException)7 FileInputStream (java.io.FileInputStream)6 Properties (java.util.Properties)6 CommandLine (org.apache.commons.cli.CommandLine)6 Option (org.apache.commons.cli.Option)6 Options (org.apache.commons.cli.Options)6 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)6 WriterAppender (org.apache.log4j.WriterAppender)6