private static void initLogger() { Logger rootLogger = Logger.getRootLogger(); PatternLayout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"); try { RollingFileAppender fileAppender = new RollingFileAppender(layout, System.getProperty("log.file", "./vnt.log")); fileAppender.setImmediateFlush(true); fileAppender.setThreshold(Level.DEBUG); fileAppender.setAppend(true); fileAppender.setMaxFileSize("5MB"); fileAppender.setMaxBackupIndex(2); rootLogger.addAppender(fileAppender); } catch (IOException e) { log.error("Failed to add appender !!", e); } }
private RollingFileAppender initLogger(String loggerName, String filePrefix) { String fileName = Paths.get(rootPath, filePrefix + "." + loggerName + ".dat").toString(); RollingFileAppender fileAppender = new RollingFileAppender(); fileAppender.setName(loggerName); fileAppender.setFile(fileName); fileAppender.setLayout(new PatternLayout("%m%n")); fileAppender.setAppend(true); fileAppender.setMaxFileSize(maxFileSize); fileAppender.setMaxBackupIndex(maxFileCount); fileAppender.activateOptions(); return fileAppender; }
private void configureAuditLogs() throws IOException { // Shutdown the LogManager to release all logger open file handles. // Unfortunately, Apache commons logging library does not provide // means to release underlying loggers. For additional info look up // commons library FAQ. LogManager.shutdown(); File file = new File(auditLogFile); if (file.exists()) { assertTrue(file.delete()); } Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger(); // disable logging while the cluster startup preps files logger.setLevel(Level.OFF); PatternLayout layout = new PatternLayout("%m%n"); RollingFileAppender appender = new RollingFileAppender(layout, auditLogFile); logger.addAppender(appender); }
/** * Configures rolling file loggers. * * @param filename the filename to output logging to. * @param loggers the logger names. */ private void configureLoggers( String filename, List<String> loggers ) { String file = getLogFile( filename ); RollingFileAppender appender = getRollingFileAppender( file ); for ( String loggerName : loggers ) { Logger logger = Logger.getRootLogger().getLoggerRepository().getLogger( loggerName ); logger.addAppender( appender ); log.info( "Added logger: " + loggerName + " using file: " + file ); } }
@SuppressWarnings("UseOfSystemOutOrSystemErr") public static void tieSystemOutAndErrToLog() { org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger(); Appender appender = rootLogger.getAppender(DT_LOG_APPENDER); if (appender instanceof RollingFileAppender) { RollingFileAppender rfa = (RollingFileAppender)appender; if (rfa.getFile() == null || rfa.getFile().isEmpty()) { rfa.setFile(System.getProperty(DT_LOGDIR)); rfa.activateOptions(); } } else if (appender != null) { logger.warn("found appender {} instead of RollingFileAppender", appender); } LoggerUtil.addAppenders(); System.setOut(createLoggingProxy(System.out)); System.setErr(createLoggingProxy(System.err)); }
/** Sets up log4j logger for auditlogs */ private void setupAuditLogs() throws IOException { // Shutdown the LogManager to release all logger open file handles. // Unfortunately, Apache commons logging library does not provide // means to release underlying loggers. For additional info look up // commons library FAQ. LogManager.shutdown(); File file = new File(auditLogFile); if (file.exists()) { assertTrue(file.delete()); } Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger(); logger.setLevel(Level.INFO); PatternLayout layout = new PatternLayout("%m%n"); RollingFileAppender appender = new RollingFileAppender(layout, auditLogFile); logger.addAppender(appender); }
@Test public void testRolloverLogfile() throws Exception { Log4jUtils.rolloverLogfile(); // add appender to cover more Logger.getLogger("somelogger"); Logger.getRootLogger().addAppender(new ConsoleAppender()); final RollingFileAppender rollingFileAppender = new RollingFileAppender(); File file = File.createTempFile("log4j", ".log"); rollingFileAppender.setFile(file.getAbsolutePath()); Logger.getRootLogger().addAppender(rollingFileAppender); Log4jUtils.rolloverLogfile(); file.deleteOnExit(); new File(file.getAbsoluteFile() + ".1").deleteOnExit(); }
/** * Create File appender, This appender is responsible to write stream of data when invoked, in our case, this appender is handeled my the Bufferappender * * @param cacheName Name of the file appender * @param fileName Filename to which is to write logs * @return returns the created appender */ private Appender CreateRollingFileAppender(String fileName) { RollingFileAppender appender = new RollingFileAppender(); appender.setName("RollingFileAppender" + _loggerName); appender.setFile(fileName); //doesnt matter since all files are created with a new name appender.setAppend(false); appender.setMaxBackupIndex(Integer.MAX_VALUE); appender.setMaxFileSize("5MB"); String str = "%-27d{ISO8601}" /*+ "\t%-45.42appdomain" + "\t%-45.42l"*/ + "\t%-42t" + "\t%-9p" + "\t%m" + "%n"; appender.setThreshold(Level.ALL); return appender; }
/** * This create log4j logger for resource access tool using root logger. * It adds file appender with specified filename to the logger which creates separate * log file for each resource access tool. * * @param clazz Class object for Resource Access Tool * @param logFile String containing name of log file. * @return log4j Logger object */ @SuppressWarnings("unchecked") public static Logger createRATSpecificLogger(Class clazz, String logFile) { Enumeration<Appender> appenders = Logger.getRootLogger() .getAllAppenders(); // Add all appenders of root logger while (appenders.hasMoreElements()) { Appender appender = appenders.nextElement(); // For file appender create new appender with different log file if (appender instanceof RollingFileAppender) { try { // Creates new file appender ((RollingFileAppender) appender).setFile(FileResourceParameters.resourceLogFolder() + logFile); ((RollingFileAppender) appender).activateOptions(); } catch (Exception e) { e.printStackTrace(); } } } return Logger.getLogger(clazz); }
private static void initLogger() { // This is the root logger provided by log4j Logger rootLogger = Logger.getRootLogger(); // Define log pattern layout PatternLayout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"); try { // Define file appender with layout and output log file name RollingFileAppender fileAppender = new RollingFileAppender(layout, "./scplayer.log"); fileAppender.setImmediateFlush(true); fileAppender.setThreshold(Level.DEBUG); fileAppender.setAppend(true); fileAppender.setMaxFileSize("5MB"); fileAppender.setMaxBackupIndex(2); // Add the appender to root logger rootLogger.addAppender(fileAppender); } catch (IOException e) { System.out.println("Failed to add appender !!"); System.exit(-1); } }
/** * initializes the log system using the logfile argument */ private void internalInit( String logfile ) throws Exception { /* * do it by our classname to avoid conflicting with anything else * that might be used... */ logger = Category.getInstance(this.getClass().getName()); logger.setAdditivity(false); /* * Priority is set for DEBUG becouse this implementation checks * log level. */ logger.setPriority(Priority.DEBUG); RollingFileAppender appender = new RollingFileAppender( new PatternLayout( "%d - %m%n"), logfile, true); appender.setMaxBackupIndex( 1 ); appender.setMaximumFileSize( 100000 ); logger.addAppender(appender); }
private void createLogger() { // Create logger synchronized (logCreatorLock) { String loggerName = System.currentTimeMillis() + "." + this.executionId + "." + this.jobId; logger = Logger.getLogger(loggerName); // Create file appender String logName = createLogFileName(node); logFile = new File(workingDir, logName); String absolutePath = logFile.getAbsolutePath(); jobAppender = null; try { RollingFileAppender fileAppender = new RollingFileAppender(loggerLayout, absolutePath, true); fileAppender.setMaxBackupIndex(jobLogBackupIndex); fileAppender.setMaxFileSize(jobLogChunkSize); jobAppender = fileAppender; logger.addAppender(jobAppender); logger.setAdditivity(false); } catch (IOException e) { flowLogger.error("Could not open log file in " + workingDir + " for job " + this.jobId, e); } } }
public void createFileLogger(String file) throws IOException { //---- ConsoleAppender console = new ConsoleAppender(); String PATTERN = "%m%n"; console.setLayout(new PatternLayout(PATTERN)); console.setThreshold(Level.INFO); console.activateOptions(); Logger.getRootLogger().getLoggerRepository().resetConfiguration(); Logger.getRootLogger().removeAllAppenders(); Logger.getRootLogger().addAppender(console); //---- org.apache.log4j.RollingFileAppender rfa = new RollingFileAppender(new org.apache.log4j.PatternLayout(),file); Logger.getRootLogger().addAppender(rfa); }
public Logger createCustomFileLogger(String file) throws IOException { //---- ConsoleAppender console = new ConsoleAppender(); String PATTERN = "%m%n-%c: "; console.setLayout(new PatternLayout(PATTERN)); console.setThreshold(Level.INFO); console.activateOptions(); Logger log = Logger.getLogger(Thread.currentThread().getName()); log.getLoggerRepository().resetConfiguration(); log.removeAllAppenders(); log.addAppender(console); //---- org.apache.log4j.RollingFileAppender rfa = new RollingFileAppender(new org.apache.log4j.PatternLayout(PATTERN),file); log.addAppender(rfa); rfa.setImmediateFlush(true); return log; }
public static void setup(Level level, String logLocation) { if (logLocation != null) { RollingFileAppender fa = new RollingFileAppender(); fa.setName("FileLogger"); fa.setFile(logLocation); fa.setMaxFileSize("5MB"); fa.setMaxBackupIndex(1); fa.setLayout(new PatternLayout("%d | %-5p| %c{1} | %m%n")); fa.setThreshold(level); fa.setAppend(true); fa.activateOptions(); Logger.getRootLogger().addAppender(fa); LOGGER.info("File Handler set"); } else { System.out.println("Log location is not set, not logging to file"); } }
@Override public void write(String loggerName, String filePrefix, String content) { RollingFileAppender logger = fileAppenders.computeIfAbsent(loggerName, f -> initLogger(loggerName, filePrefix)); LoggingEvent event = new LoggingEvent(loggerName, Logger.getLogger(loggerName), Level.ALL, content, null); logger.append(event); }
private static void initAppenders() throws IOException { systemLogAppender = new RollingFileAppender(getLayout(), logFilePath + File.separatorChar + "system.log"); accessLogAppender = new RollingFileAppender(getLayout(), logFilePath + File.separatorChar + "access.log"); auditLogAppender = new RollingFileAppender(getLayout(), logFilePath + File.separatorChar + "audit.log"); reverseProxyLogAppender = new RollingFileAppender(getLayout(), logFilePath + File.separatorChar + "reverseproxy.log"); // setting the max backup index and file size systemLogAppender.setMaxBackupIndex(MAX_BACKUP_INDEX); systemLogAppender.setMaxFileSize(MAX_FILE_SIZE); systemLogAppender.setName(systemLogAppenderName); accessLogAppender.setMaxBackupIndex(MAX_BACKUP_INDEX); accessLogAppender.setMaxFileSize(MAX_FILE_SIZE); accessLogAppender.setName(accessLogAppenderName); auditLogAppender.setMaxBackupIndex(MAX_BACKUP_INDEX); auditLogAppender.setMaxFileSize(MAX_FILE_SIZE); auditLogAppender.setName(auditLogAppenderName); reverseProxyLogAppender.setMaxBackupIndex(MAX_BACKUP_INDEX); reverseProxyLogAppender.setMaxFileSize(MAX_FILE_SIZE); reverseProxyLogAppender.setName(reverseProxyLogAppenderName); }
private static void changeFileAppenderIfNew(Logger logger, String fileAppenderName, RollingFileAppender newFileAppender) { Appender existingFileAppender = logger.getAppender(fileAppenderName); if (existingFileAppender == null) { logger.removeAppender(consoleAppender); logger.addAppender(newFileAppender); } else if (existingFileAppender != newFileAppender) { logger.removeAppender(existingFileAppender); logger.addAppender(newFileAppender); } return; }
/** Sets up log4j logger for auditlogs */ private void setupAuditLogs() throws IOException { File file = new File(auditLogFile); if (file.exists()) { file.delete(); } Logger logger = ((Log4JLogger) FSNamesystem.auditLog).getLogger(); logger.setLevel(Level.INFO); PatternLayout layout = new PatternLayout("%m%n"); RollingFileAppender appender = new RollingFileAppender(layout, auditLogFile); logger.addAppender(appender); }
private void initializeLogger() { DateFormat dateFormat = new SimpleDateFormat("yyyy-dd-MM"); Date date = new Date(); String curDate = dateFormat.format(date); MethodAnalysis meth = new MethodAnalysis(); String filename = meth.getFilePath()+"logs/SCM_logFile_"+curDate+".log";//+"_"+log_number+".log"; try { PatternLayout layout = new PatternLayout(); layout.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"); appender = new RollingFileAppender(); appender.setAppend(true); appender.setMaxFileSize("10MB"); appender.setMaxBackupIndex(2); appender.setFile(filename); appender.setLayout(layout); appender.activateOptions(); LOGGER.addAppender(appender); } catch(Exception e) { System.out.println("Exception in loading the logger in the Home"); } }
/** * Configures a root file logger. * * @param filename the filename to output logging to. */ private void configureRootLogger( String filename ) { String file = getLogFile( filename ); RollingFileAppender appender = getRollingFileAppender( file ); Logger.getRootLogger().addAppender( appender ); log.info( "Added root logger using file: " + file ); }
/** * Returns a rolling file appender. * * @param file the file to output to, including path and filename. */ private RollingFileAppender getRollingFileAppender( String file ) { RollingFileAppender appender = new RollingFileAppender(); appender.setThreshold( Level.INFO ); appender.setFile( file ); appender.setMaxFileSize( MAX_FILE_SIZE ); appender.setLayout( PATTERN_LAYOUT ); appender.activateOptions(); return appender; }
public Log4jFileLoggerFactory(File logFile, String categoriesWithDebugLevel) throws IOException { myCategoriesWithDebugLevel = categoriesWithDebugLevel.isEmpty() ? Collections.<String>emptyList() : Arrays.asList(categoriesWithDebugLevel.split(",")); PatternLayout pattern = new PatternLayout("%d [%7r] %6p - %30.30c - %m\n"); myAppender = new RollingFileAppender(pattern, logFile.getAbsolutePath()); myAppender.setMaxFileSize("20MB"); myAppender.setMaxBackupIndex(10); }
@BeforeClass public static void beforeClass() throws IOException { String logFileDir = new File("target" + File.separator + "logDir").getAbsolutePath(); logFileName = logFileDir + File.separator + "appTest.log"; RollingFileAppender rfa = new RollingFileAppender(new PatternLayout("%d{ISO8601} [%t] %-5p %c{2} %M - %m%n"), logFileName); rfa.setName(APPENDER_NAME); logger.addAppender(rfa); }
@Test public void testImmediateFlushOff() { RollingFileAppender rfa = (RollingFileAppender)logger.getAppender(APPENDER_NAME); assertTrue(rfa.getImmediateFlush()); rfa.setImmediateFlush(false); assertNull(LoggerUtil.getLogFileInformation()); rfa.setImmediateFlush(true); }
@Test public void testErrorLevelOff() { RollingFileAppender rfa = (RollingFileAppender)logger.getAppender(APPENDER_NAME); assertNull(rfa.getThreshold()); rfa.setThreshold(Level.FATAL); assertNull(LoggerUtil.getLogFileInformation(logger)); rfa.setThreshold(null); }
@Test public void testNoFileAppender() { RollingFileAppender rfa = (RollingFileAppender)logger.getAppender(APPENDER_NAME); logger.removeAppender(APPENDER_NAME); assertNull(LoggerUtil.getLogFileInformation()); logger.addAppender(rfa); }
@Test public void testLog4jJsonConfiguration() throws Exception { JSONObject json = createJSONObject(); new Log4jJSONConfigurator().doConfigure(json, LogManager.getLoggerRepository()); Logger log = LOG.getRootLogger(); assertEquals("failure - level does not match", log.getEffectiveLevel(), Level.INFO); Appender console_test = log.getAppender("console_test"); assertTrue("failure - no console_test appender configured", console_test != null); assertEquals("failure - console_test logger has the wrong class", ConsoleAppender.class, console_test.getClass()); assertEquals("failure - console_test logger target is wrong", "System.err", ((ConsoleAppender) console_test).getTarget()); assertEquals("failure - console_test logger layout is wrong", PatternLayout.class, console_test .getLayout().getClass()); assertTrue("failure - console_test logger layout.ConversionPattern is wrong", "%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n".equals(((PatternLayout) console_test.getLayout()) .getConversionPattern())); Appender rfa = log.getAppender("RFA"); assertTrue("failure - no RFA appender configured", rfa != null); assertEquals("failure - RFA logger has the wrong class", RollingFileAppender.class, rfa.getClass()); // assertEquals("failure - RFA logger file is wrong", "./hadoop.log", // ((RollingFileAppender) rfa).getFile()); assertEquals("failure - RFA logger MaxFileSize is wrong", 1048576l, ((RollingFileAppender) rfa).getMaximumFileSize()); assertEquals("failure - RFA logger MaxBackupIndex is wrong", 30, ((RollingFileAppender) rfa).getMaxBackupIndex()); assertEquals("failure - RFA logger layout is wrong", PatternLayout.class, rfa.getLayout() .getClass()); assertTrue("failure - RFA logger layout.ConversionPattern is wrong", "%d{ISO8601} %-5p %c{2} - %m%n".equals(((PatternLayout) rfa.getLayout()) .getConversionPattern())); }