Right now I am trying use log4j2 to log everything that has a level of INFO or higher (WARN, ERROR, AND FATAL) to my server and anything that has a level of INFO to my console. I am able to log things to my console, however, I am having an issue logging the correct levels to the server properly.
Here is what I have tried so far:
Java
import java.time.Instant;
import org.apache.log4j.PropertyConfigurator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class App {
private static final Logger log4j = LogManager.getLogger(App.class.getName());
public static void main(String[] args) {
try {
String log4jConfPath = "src/main/resources/log4j2.xml";
PropertyConfigurator.configure(log4jConfPath);
log4j.info("this is a testmessage " + Instant.now().toString());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
XML
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" packages="org.graylog2.log4j2">
<Properties>
<Property name="default_pattern">%d{MM/dd/yyyy hh:mm:ss} %5p %c{1} - %m%n
</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${default_pattern}" />
</Console>
<GELF name="gelfAppender" server="graylog.x.something.com"
hostName="some.host" port="12201">
<PatternLayout pattern="%d{dd MMM yyyy HH:mm:ss,SSS} %5p %c{1} - %m%n" />
<KeyValuePair key="extractStacktrace" value="true" />
<KeyValuePair key="addExtendedInformation" value="true" />
<KeyValuePair key="facility" value="gelf-java" />
<KeyValuePair key="environment" value="TEST" />
<KeyValuePair key="application" value="MyApp" />
<KeyValuePair key="additionalFields" value="{'environment': 'TEST', 'application': 'MyAPP'}" />
</GELF>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="gelfAppender" />
<AppenderRef ref="console" />
</Root>
<Root level="info">
<AppenderRef ref="gelfAppender" />
<AppenderRef ref="console" />
</Root>
</Loggers>
</Configuration>
The above code does not output what I want, which is to have INFO level output to the console and INFO Levels and up output to the server.
Instead, I am able to output: EVERYTHING (Trace) to console and INFO to the server.
I have messed around with the XML file a little bit and I noticed that when I change
status="trace"
to
status="off"
it logs only INFO to the console, but nothing to the server.
Lastly, and probably the most odd thing of all, if I remove
Instant.now().toString()
from my print statement, then nothing will be logged to the server, regardless of the status (if it's TRACE or OFF), but it still logs to the console. I thought it had something to do with the pattern layout of my GELF appender, so I changed
<PatternLayout pattern="%d{dd MMM yyyy HH:mm:ss,SSS} %5p %c{1} - %m%n" />
to
<PatternLayout pattern="${default_pattern}" />
but that did not change the output...
Here are my current dependencies:
In short, I just want to log levels of INFO to my console and levels of INFO or higher to my server.