Search in sources :

Example 31 with FileAppender

use of org.apache.log4j.FileAppender in project smscgateway by RestComm.

the class TestHarness method init.

public void init() {
    try {
        Properties tckProperties = new Properties();
        InputStream inStreamLog4j = TestHarness.class.getResourceAsStream("/log4j.properties");
        System.out.println("Input Stream = " + inStreamLog4j);
        Properties propertiesLog4j = new Properties();
        try {
            propertiesLog4j.load(inStreamLog4j);
            PropertyConfigurator.configure(propertiesLog4j);
        } catch (IOException e) {
            e.printStackTrace();
            BasicConfigurator.configure();
        }
        logger.debug("log4j configured");
        String lf = System.getProperties().getProperty(LOG_FILE_NAME);
        if (lf != null) {
            logFileName = lf;
        }
        // If already created a print writer then just use it.
        try {
            logger.addAppender(new FileAppender(new SimpleLayout(), logFileName));
        } catch (FileNotFoundException fnfe) {
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }
}
Also used : FileAppender(org.apache.log4j.FileAppender) SimpleLayout(org.apache.log4j.SimpleLayout) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 32 with FileAppender

use of org.apache.log4j.FileAppender in project smscgateway by RestComm.

the class SMPPp method initLog4J.

private void initLog4J(Properties props) {
    try {
        String log4jProps = props.getProperty("smppp.log4jPropsFilePath");
        if (log4jProps != null) {
            InputStream inStreamLog4j = new FileInputStream(new File(log4jProps));
            Properties propertiesLog4j = new Properties();
            propertiesLog4j.load(inStreamLog4j);
            PropertyConfigurator.configure(propertiesLog4j);
        } else {
            BasicConfigurator.configure();
        }
        String lf = props.getProperty("smppp.logFilePath");
        if (lf != null) {
            ctx.logger.addAppender(new FileAppender(new SimpleLayout(), lf));
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : FileAppender(org.apache.log4j.FileAppender) SimpleLayout(org.apache.log4j.SimpleLayout) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 33 with FileAppender

use of org.apache.log4j.FileAppender 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 34 with FileAppender

use of org.apache.log4j.FileAppender in project atlas by apache.

the class FailedMessagesLogger method getRootLoggerDirectory.

/**
 * Get the root logger file location under which the failed log messages will be written.
 *
 * Since this class is used in Hooks which run within JVMs of other components like Hive,
 * we want to write the failed messages file under the same location as where logs from
 * the host component are saved. This method attempts to get such a location from the
 * root logger's appenders. It will work only if at least one of the appenders is a {@link FileAppender}
 *
 * @return directory under which host component's logs are stored.
 */
private String getRootLoggerDirectory() {
    String rootLoggerDirectory = null;
    org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
    Enumeration allAppenders = rootLogger.getAllAppenders();
    if (allAppenders != null) {
        while (allAppenders.hasMoreElements()) {
            Appender appender = (Appender) allAppenders.nextElement();
            if (appender instanceof FileAppender) {
                FileAppender fileAppender = (FileAppender) appender;
                String rootLoggerFile = fileAppender.getFile();
                rootLoggerDirectory = new File(rootLoggerFile).getParent();
                break;
            }
        }
    }
    return rootLoggerDirectory;
}
Also used : DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) FileAppender(org.apache.log4j.FileAppender) Logger(org.apache.log4j.Logger) Enumeration(java.util.Enumeration) File(java.io.File)

Example 35 with FileAppender

use of org.apache.log4j.FileAppender in project com.revolsys.open by revolsys.

the class ScriptTool method run.

private void run() {
    final long startTime = System.currentTimeMillis();
    final ThreadLocalFileAppender localAppender = ThreadLocalFileAppender.getAppender();
    if (localAppender != null && this.logFile != null) {
        final File parentFile = this.logFile.getParentFile();
        if (parentFile != null) {
            parentFile.mkdirs();
        }
        localAppender.setLocalFile(this.logFile.getAbsolutePath());
    } else if (this.logFile != null) {
        final org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
        try {
            final Layout layout = new PatternLayout("%d\t%p\t%m%n");
            final Appender appender = new FileAppender(layout, this.logFile.getAbsolutePath(), false);
            rootLogger.addAppender(appender);
        } catch (final IOException e) {
            final Layout layout = new PatternLayout("%p\t%m%n");
            final Appender appender = new ConsoleAppender(layout);
            rootLogger.addAppender(appender);
            Logs.error(this, "Cannot find log file " + this.logFile, e);
        }
    }
    final StringBuilder message = new StringBuilder("Processing ");
    message.append(" -s ");
    message.append(this.scriptFileName);
    if (this.propertiesName != null) {
        message.append(" -p ");
        message.append(this.propertiesName);
    }
    for (final Entry<String, String> parameter : this.parameters.entrySet()) {
        message.append(" ");
        message.append(parameter.getKey());
        message.append("=");
        message.append(parameter.getValue());
    }
    System.out.println(message);
    if (System.getProperty("applicationHome") == null) {
        ThreadSharedProperties.setProperty("applicationHome", ".");
        System.setProperty("applicationHome", ".");
    }
    try {
        final GenericApplicationContext beans = new GenericApplicationContext();
        AnnotationConfigUtils.registerAnnotationConfigProcessors(beans, null);
        beans.getBeanFactory().addPropertyEditorRegistrar(new ResourceEditorRegistrar());
        if (this.scriptFile != null) {
            new XmlBeanDefinitionReader(beans).loadBeanDefinitions("file:" + this.scriptFile.getAbsolutePath());
        } else {
            new XmlBeanDefinitionReader(beans).loadBeanDefinitions("classpath:" + this.scriptFileName);
        }
        beans.refresh();
        try {
            Logs.info(this, message.toString());
            final Object bean = beans.getBean("processNetwork");
            final ProcessNetwork pipeline = (ProcessNetwork) bean;
            pipeline.startAndWait();
        } finally {
            beans.close();
        }
    } catch (final BeanCreationException e) {
        final Throwable cause = getBeanExceptionCause(e);
        Logs.error(this, cause.getMessage(), cause);
        cause.printStackTrace();
        System.err.flush();
    }
    final long endTime = System.currentTimeMillis();
    final long time = endTime - startTime;
    long seconds = time / 1000;
    final long minutes = seconds / 60;
    seconds = seconds % 60;
    Logs.info(this, minutes + " minutes " + seconds + " seconds");
    System.out.println(minutes + " minutes " + seconds + " seconds");
}
Also used : Appender(org.apache.log4j.Appender) ThreadLocalFileAppender(com.revolsys.logging.log4j.ThreadLocalFileAppender) ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) ThreadLocalFileAppender(com.revolsys.logging.log4j.ThreadLocalFileAppender) FileAppender(org.apache.log4j.FileAppender) ConsoleAppender(org.apache.log4j.ConsoleAppender) BeanCreationException(org.springframework.beans.factory.BeanCreationException) ResourceEditorRegistrar(com.revolsys.beans.propertyeditor.ResourceEditorRegistrar) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) PatternLayout(org.apache.log4j.PatternLayout) ProcessNetwork(com.revolsys.parallel.process.ProcessNetwork) IOException(java.io.IOException) ThreadLocalFileAppender(com.revolsys.logging.log4j.ThreadLocalFileAppender) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) File(java.io.File)

Aggregations

FileAppender (org.apache.log4j.FileAppender)47 Appender (org.apache.log4j.Appender)18 File (java.io.File)15 IOException (java.io.IOException)14 PatternLayout (org.apache.log4j.PatternLayout)14 Logger (org.apache.log4j.Logger)12 ConsoleAppender (org.apache.log4j.ConsoleAppender)8 Layout (org.apache.log4j.Layout)5 Enumeration (java.util.Enumeration)4 Properties (java.util.Properties)4 FileNotFoundException (java.io.FileNotFoundException)3 DailyRollingFileAppender (org.apache.log4j.DailyRollingFileAppender)3 RollingFileAppender (org.apache.log4j.RollingFileAppender)3 SimpleLayout (org.apache.log4j.SimpleLayout)3 InputStream (java.io.InputStream)2 HTMLLayout (org.apache.log4j.HTMLLayout)2 LoggingEvent (org.apache.log4j.spi.LoggingEvent)2 LoggerChannel (com.biglybt.pif.logging.LoggerChannel)1 LoggerChannelListener (com.biglybt.pif.logging.LoggerChannelListener)1 LogProfileInfo (com.creditease.uav.profiling.handlers.log.LogProfileInfo)1