2

I am getting an extra empty file when I try to get my logs from log4j2. The file name is "$(sys" and it contains absolutely nothing but it is created every time I run my program here is my java file where I log:

public static void main(String[] args){

    Logger logger = LogManager.getLogger();

    final Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(System.currentTimeMillis());
    Date date = cal.getTime();
    int mHour = date.getHours();
    int mMinute = date.getMinutes();

    String filenameWE = String.valueOf(mHour) + "_" + String.valueOf(mMinute)+"_WarErr";
    String filenameFull = String.valueOf(mHour) + "_" + String.valueOf(mMinute)+"_Full_Log";

    System.setProperty("WarErrFilename", filenameWE);
    System.setProperty("FullLogFilename", filenameFull);

    LoggerContext ctx =  (LoggerContext) LogManager.getContext(false);
    ctx.reconfigure();

    logger.debug("Hello world - debug log");
    logger.debug("Hello world - debug log");
    logger.debug("Hello world - debug log");
    logger.info("Hello world - info log");
    logger.info("Hello world - info log");
    logger.info("Hello world - info log");

    logger.warn("Hello world - warn log");        
    logger.warn("I farted!");
    logger.error("Hello world - error log");
    logger.error("Error please insert brain!");
    logger.error("Error cant poop!");
}

And my log4j2.xml

<?xml version="1.0" encoding="UTF-8"?><Configuration status="INFO">
<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
    </Console>

    <File name="MyFile" fileName="logs/${date:yyyy}/${date:MM}/${date:dd}/${sys:WarErrFilename}.log" immediateFlush="false" append="false">
        <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
        <Filters>
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="NEUTRAL" />
            <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="NEUTRAL" />
            <ThresholdFilter level="info" onMatch="DENY" onMismatch="DENY" />
        </Filters>
    </File>
    <File name="Technical"
          fileName="logs/${date:yyyy}/${date:MM}/${date:dd}/${sys:FullLogFilename}.log">
        <PatternLayout pattern="%d %p %c{1.} [%t] %m%n" />
    </File>
</Appenders>

<Loggers>
    <Root level="debug">
        <AppenderRef ref="Console" />
        <AppenderRef ref="MyFile"/>
        <AppenderRef ref="Technical"/>
    </Root>
</Loggers>  
</Configuration>
2
  • Do both two (MyFile & Technical) logfiles exist? This would help narrow down your error. Commented Apr 6, 2016 at 14:13
  • Yes the 2 files do exist. Sorry for the late response Commented Apr 7, 2016 at 9:14

1 Answer 1

2

Your config is missing the outer <Configuration> element that your <Appenders> and <Loggers> must be nested in. Take a look at the manual for an example.

Also, why not set the system properties before getting the Logger from the LogManager?

Sign up to request clarification or add additional context in comments.

6 Comments

if you take a closer look at the code I provided, the <Configuration> element is there
Oh I see. On my phone it's hidden behind the <?xml...?> element. But I don't see a closing tag...
its there I just need to edit it else it wont compile and produce any result now would it? :) I will fix it up in sec.
Now it should all be fine.
Have you tried setting the necessary system properties before initializing Log4j by getting the first Logger from the LogManager? Then you don't need to reconfigure. (There may be a bug in the reconfigure logic...)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.