Search in sources :

Example 56 with PatternLayout

use of org.apache.log4j.PatternLayout in project cpsolver by UniTime.

the class Test method setupLogging.

/**
     * Setup log4j logging
     * 
     * @param logFile
     *            log file
     * @param debug
     *            true if debug messages should be logged (use -Ddebug=true to
     *            enable debug message)
     */
public static void setupLogging(File logFile, boolean debug) {
    Logger root = Logger.getRootLogger();
    ConsoleAppender console = new ConsoleAppender(new PatternLayout("[%t] %m%n"));
    console.setThreshold(Level.INFO);
    root.addAppender(console);
    if (logFile != null) {
        try {
            FileAppender file = new FileAppender(new PatternLayout("%d{dd-MMM-yy HH:mm:ss.SSS} [%t] %-5p %c{2}> %m%n"), logFile.getPath(), false);
            file.setThreshold(Level.DEBUG);
            root.addAppender(file);
        } catch (IOException e) {
            sLog.fatal("Unable to configure logging, reason: " + e.getMessage(), e);
        }
    }
    if (!debug)
        root.setLevel(Level.INFO);
}
Also used : ConsoleAppender(org.apache.log4j.ConsoleAppender) FileAppender(org.apache.log4j.FileAppender) PatternLayout(org.apache.log4j.PatternLayout) IOException(java.io.IOException) Logger(org.apache.log4j.Logger)

Example 57 with PatternLayout

use of org.apache.log4j.PatternLayout in project suite by stupidsing.

the class LogUtil method initLog4j.

public static void initLog4j(Level level) {
    Path logDir = Constants.tmp("logs");
    PatternLayout layout = new PatternLayout("%d %-5p [%c{1}] %m%n");
    ConsoleAppender console = new ConsoleAppender(layout);
    console.setWriter(new PrintWriter(System.err));
    console.activateOptions();
    DailyRollingFileAppender file = new DailyRollingFileAppender();
    file.setFile(logDir.resolve("suite.log").toString());
    file.setDatePattern("'.'yyyyMMdd");
    file.setLayout(layout);
    file.activateOptions();
    Logger logger = Logger.getRootLogger();
    logger.setLevel(level);
    logger.removeAllAppenders();
    logger.addAppender(console);
    logger.addAppender(file);
}
Also used : Path(java.nio.file.Path) ConsoleAppender(org.apache.log4j.ConsoleAppender) PatternLayout(org.apache.log4j.PatternLayout) Logger(org.apache.log4j.Logger) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) PrintWriter(java.io.PrintWriter)

Example 58 with PatternLayout

use of org.apache.log4j.PatternLayout in project traccar by tananaev.

the class Log method setupLogger.

public static void setupLogger(Config config) throws IOException {
    Layout layout = new PatternLayout("%d{" + DATE_FORMAT + "} %5p: %m%n");
    Appender appender = new DailyRollingFileAppender(layout, config.getString("logger.file"), "'.'yyyyMMdd");
    LogManager.resetConfiguration();
    LogManager.getRootLogger().addAppender(new NullAppender());
    logger = Logger.getLogger(LOGGER_NAME);
    logger.addAppender(appender);
    logger.setLevel(Level.toLevel(config.getString("logger.level"), Level.ALL));
    // Workaround for "Bug 745866 - (EDG-45) Possible netty logging config problem"
    InternalLoggerFactory.setDefaultFactory(new InternalLoggerFactory() {

        @Override
        public InternalLogger newInstance(String string) {
            return new NettyInternalLogger();
        }
    });
    Log.logSystemInfo();
    Log.info("Version: " + getAppVersion());
}
Also used : NullAppender(org.apache.log4j.varia.NullAppender) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) Appender(org.apache.log4j.Appender) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) NullAppender(org.apache.log4j.varia.NullAppender) PatternLayout(org.apache.log4j.PatternLayout) InternalLoggerFactory(org.jboss.netty.logging.InternalLoggerFactory) DailyRollingFileAppender(org.apache.log4j.DailyRollingFileAppender) InternalLogger(org.jboss.netty.logging.InternalLogger) AbstractInternalLogger(org.jboss.netty.logging.AbstractInternalLogger)

Example 59 with PatternLayout

use of org.apache.log4j.PatternLayout in project uavstack by uavorg.

the class Log4jHookProxy method figureoutLogConfiguration.

@SuppressWarnings("unchecked")
private void figureoutLogConfiguration(Logger Logger, LinkedList<LogProfileInfo> list, String appid) {
    Enumeration<Appender> appenders = Logger.getAllAppenders();
    while (appenders != null && appenders.hasMoreElements()) {
        LogProfileInfo logProfileInfo = new LogProfileInfo();
        if (appid != null) {
            logProfileInfo.setAppId(appid);
        }
        logProfileInfo.setLogType(LogProfileInfo.LogType.Log4j);
        Map<String, String> attributes = new HashMap<String, String>();
        attributes.put(LogProfileInfo.ENGINE, "log4j");
        Appender appender = appenders.nextElement();
        if (!(appender instanceof FileAppender)) {
            continue;
        }
        FileAppender fileAppender = (FileAppender) appender;
        Layout layout = fileAppender.getLayout();
        if (null != layout) {
            if (layout instanceof PatternLayout) {
                PatternLayout patternLayout = (PatternLayout) fileAppender.getLayout();
                attributes.put(LogProfileInfo.PATTERN, patternLayout.getConversionPattern());
            } else if (layout instanceof HTMLLayout) {
                attributes.put(LogProfileInfo.PATTERN, "HTMLLayout");
            } else if (layout instanceof SimpleLayout) {
                attributes.put(LogProfileInfo.PATTERN, "SimpleLayout");
            } else if (layout instanceof TTCCLayout) {
                attributes.put(LogProfileInfo.PATTERN, "TTCCLayout");
            }
        }
        boolean useBuffIO = fileAppender.getBufferedIO();
        if (useBuffIO == true) {
            attributes.put(LogProfileInfo.BUFFER_SIZE, String.valueOf(fileAppender.getBufferSize()));
            attributes.put(LogProfileInfo.BUFFRT_IO, String.valueOf(useBuffIO));
        }
        logProfileInfo.setFilePath(fileAppender.getFile());
        logProfileInfo.setAttributes(attributes);
        list.add(logProfileInfo);
    }
}
Also used : Appender(org.apache.log4j.Appender) FileAppender(org.apache.log4j.FileAppender) FileAppender(org.apache.log4j.FileAppender) HashMap(java.util.HashMap) HTMLLayout(org.apache.log4j.HTMLLayout) PatternLayout(org.apache.log4j.PatternLayout) LogProfileInfo(com.creditease.uav.profiling.handlers.log.LogProfileInfo) SimpleLayout(org.apache.log4j.SimpleLayout) TTCCLayout(org.apache.log4j.TTCCLayout) HTMLLayout(org.apache.log4j.HTMLLayout) SimpleLayout(org.apache.log4j.SimpleLayout) Layout(org.apache.log4j.Layout) PatternLayout(org.apache.log4j.PatternLayout) TTCCLayout(org.apache.log4j.TTCCLayout)

Example 60 with PatternLayout

use of org.apache.log4j.PatternLayout in project xian by happyyangyuan.

the class GelfLog4j1Init method addGelfAppender.

private static void addGelfAppender() {
    GelfAppender appender = new GelfJsonAppender();
    appender.setName(GELF_APPENDER_NAME);
    if (EnvUtil.isLan()) {
        appender.setGraylogHost(XianConfig.get("gelfInputLanUrl"));
    } else {
        appender.setGraylogHost(XianConfig.get("gelfInputInternetUrl"));
    }
    appender.setGraylogPort(XianConfig.getIntValue("gelfInputInternetUrl"));
    appender.setFacility("gelf-java");
    appender.setLayout(new PatternLayout() {

        {
            setConversionPattern("%p %m");
        }
    });
    appender.setExtractStacktrace(true);
    appender.setIncludeLocation(false);
    JSONObject additionalFields = new JSONObject() {

        {
            put("environment", EnvUtil.getEnv());
            put("application", EnvUtil.getApplication());
            put("pid", JavaPIDUtil.getProcessName());
            put("nodeId", LocalNodeManager.LOCAL_NODE_ID);
        }
    };
    appender.setAddExtendedInformation(true);
    appender.setAdditionalFields(additionalFields.toJSONString());
    appender.setOriginHost(JavaPIDUtil.getHostname());
    // 让以上设置生效
    appender.activateOptions();
    Logger.getRootLogger().addAppender(appender);
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) PatternLayout(org.apache.log4j.PatternLayout) GelfAppender(org.graylog2.log.GelfAppender) GelfJsonAppender(org.graylog2.log.GelfJsonAppender)

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