0

I am using log4j programmatically. It seems to work outside of console messages. System.out messages do not appear. logger.info/error/etc all appear in file. Can you not log System.out information? What am I doing wrong?

   Logger logger = Logger.getRootLogger();
            logger = Logger.getLogger("com.mycodebase");

  ConsoleAppender consoleAppender = new ConsoleAppender();
        PatternLayout consolePatternLayout = new PatternLayout();
        consolePatternLayout.setConversionPattern("%d{dd MMM yyyy} %d{HH:mm:ss}: %5p %c{1}:%L - %m%n");
        consoleAppender.setLayout(consolePatternLayout);
        consoleAppender.setName("Iridium Suite Console appender");
        consoleAppender.setThreshold(Level.INFO);
        consoleAppender.activateOptions();

        //Create Iridium Suite log file appender
        isAppender = new RollingFileAppender();
        isAppender.setFile(logFileName);
        PatternLayout isPatternLayout = new PatternLayout();
        isPatternLayout.setConversionPattern("%d{dd MMM yyyy} %d{HH:mm:ss}: %5p %c{1}:%L - %m%n");
        isAppender.setLayout(isPatternLayout);
        isAppender.setThreshold(Level.INFO);
        isAppender.setName("log appender");
        isAppender.setAppend(true);
        isAppender.setMaxBackupIndex(0);
        isAppender.setMaxFileSize("2MB");
        isAppender.activateOptions();

        Logger.getRootLogger().addAppender(consoleAppender);
        Logger.getRootLogger().addAppender(isAppender);
springcorn
  • 611
  • 2
  • 15
  • 28

1 Answers1

1

ConsoleAppender is used to direct your log messages to stdout but not the other way around. People have come up with good solutions here: log4j redirect stdout to DailyRollingFileAppender

Community
  • 1
  • 1
h3X3n
  • 73
  • 1
  • 5
  • Thanks. I guess I am starting to understand that System.etc and log4j are alternative implementations not complementary. I wanted system.out to integrate into log4j not really the opposite. I see how I can use this example you linked to, but I am not sure I like that kind of heavy handed redirection. I worry my redirection used for debugging will create its own problems. – springcorn Jul 27 '16 at 22:43
  • Well in order to simplify that process you could simply replace all the System.out.prints in your code by logger.info or logger.debug or something and it would essentially have the same effect since you are already directing your logs to stdout. Though if you want to use logger.debug you'll have to lower your threshold levels. – h3X3n Jul 28 '16 at 13:17
  • Yes that is what I am realizing. But it is unfortunate there isn't a way to say logger.setAppendAllSystemOutput(true); – springcorn Jul 28 '16 at 17:32