I have a method foo which is called inside a loop over a list of objects.
class Bar{
private static final Logger logger = LoggerFactory.getLogger(Bar.class);
void foo(A obj){
//bunch of debug logs
logger.debug("A");
logger.debug("B");
//..and so on
//processing functionality
}
}
Now, whenever the object is processed successfully, the debug logs are printed in the log file. But if an object throws an error while processing, the debug logs are not getting printed.
I am using log4j2 and my log4j.xml file is below:
<Configuration monitorInterval="60" status="WARN">
<Loggers>
<Logger name="com.abc" level="INFO"/>
</Loggers>
<Loggers>
<Root level="INFO">
<AppenderRef ref="console"/>
<AppenderRef ref="asyncFileLogger"/>
</Root>
</Loggers>
</Configuration>
What could be the issue behind this?
Related
Currently, our application writes logs to Splunk, and we don't write to a file.
We have a new requirement whereby I need to write logs to a file when, and only when, there are one or more issues in writing directly to Splunk (Splunk is down, Connectivity issue, etc.)
How can I configure log4j2 to write locally if, and only if, the remote Splunk listener is not available?
Modify log4j2.xml
...
<Appenders>
...
<SplunkHttp name="Splunk" ... />
<RollingFile name="BackupFileLog" ... />
...
</Appenders>
...
<Loggers>
<Root level="INFO">
<AppenderRef ref="Splunk" />
</Root>
<Logger name="BackupFileLog" level="INFO" additivity="false">
<AppenderRef ref="BackupFileLog" />
</Logger>
...
</Loggers>
Prepare SplunkErrorHandler
public class SplunkErrorHandler implements HttpEventCollectorErrorHandler.ErrorCallback {
private static Logger BACKUP_LOGGER = LogManager.getLogger("BackupFileLog");
public void error(final List<HttpEventCollectorEventInfo> data, final Exception ex) {
// Here you can forward Splunk error and not delivered messages to BACKUP_LOGGER
...
}
}
Inject SplunkErrorHandler
public static void main(String[] args) {
HttpEventCollectorErrorHandler.onError(new SplunkErrorHandler());
...
}
Some key points
additivity="false" in logger is important
name="BackupFileLog" logger should not be binded to class
LogManager.getLogger("BackupFileLog") backup logger should be requested by name
I would like to print all INFO logs to my console, but only send ERROR logs to my Airbrake appender.
My log4j2 has a root logger that adds the ConsoleAppender and is set to log level INFO.
<Loggers>
<Root level="INFO">
<AppenderRef ref="ConsoleAppender"/>
</Root>
</Loggers>
I then create a logger by the name of my application com.acme.app
Logger logger = LogManager.getLogger("com.acme.app");
logger.info("This is an info log");
logger.error("This is an error log");
With this configuration, when I run my application I will get the output
2019-06-10 12:20:48 [main] INFO ... - This is an info log
2019-06-10 12:20:48 [main] ERROR ... - This is an error log
I now want to add the airbrake appender and only send ERROR logs to airbrake, but I want to continue printing both INFO and ERROR logs.
<Loggers>
<Root level="INFO">
<AppenderRef ref="ConsoleAppender"/>
</Root>
<Logger name="com.acme.app" level="ERROR">
<AppenderRef ref="Airbrake"/>
</Logger>
</Loggers>
This will start sending ERROR logs to my airbrake, but now I will only see ERROR logs in the console.
If I set the level to INFO or move the appender to the root, I will see both INFO and ERROR logs, but I will also be sending INFO logs to airbrake- which is undesirable.
I've also tried adding multiple levels for the same logger name and explicitly point INFO back to the console-
<Loggers>
<Root level="INFO">
<AppenderRef ref="ConsoleAppender"/>
</Root>
<Logger name="com.acme.app" level="ERROR">
<AppenderRef ref="Airbrake"/>
</Logger>
<Logger name="com.acme.app" level="INFO">
<AppenderRef ref="ConsoleAppender"/>
</Logger>
</Loggers>
I tried both ERROR first and INFO first and it appears whichever is the last defined is what's used. If INFO comes last it will print all of the logs but not sending any errors, if ERROR comes last it will send the error but not print any of the INFO logs.
How can I have Airbrake capture all of the ERROR logs, but allow other logs to fallback to the root logger without sending all logs to the Airbrake appender?
In simple terms what is the result of making additivity="true" or additivity="false" when adding a Log4j configuration for a specific class like this?
<Logger name="com.mypage.glass.TryWindow" level="INFO" additivity="true">
<AppenderRef ref="console"/>
<AppenderRef ref="file"/>
</Logger>
By default, a logger inherits the appenders from its ancestors. By setting additivity="false", you prevent this behaviour.
In your example, there may be appenders associated with com.mypage.glass or com.mypage or even the root logger that would be inherited if you don't set that property to false.
I recognized some problems with the SMTPAppender in log4j2. Whenever log events with the level error or fatal are created without having an event with the level info before no mail is sent and the fatal event disappears.
Here is my log4j2 configuration file (log4j2.xml) and a small program (LogTest.java) to reproduce the problem:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn">
<!-- mail server configuration -->
<properties>
<property name="receipients">me#example.com</property>
<property name="from">me#example.com</property>
<property name="smtpHost">smtp.example.com</property>
<property name="smtpPort">25</property>
<property name="smtpProtocol">smtp</property>
<property name="smtpUser">me</property>
<property name="smtpPassword">secret</property>
</properties>
<appenders>
<!-- appender to write all info events to stdout -->
<Console name="Console" target="SYSTEM_OUT">
<ThresholdFilter level="info" onMatch="NEUTRAL" onMismatch="DENY"/>
</Console>
<!-- appender to send mails (default: error and fatal events)-->
<SMTP name="Mailer" suppressExceptions="false"
subject="Error log" to="${receipients}" from="${from}"
smtpHost="${smtpHost}" smtpPort="${smtpPort}"
smtpProtocol="${smtpProtocol}" smtpUsername="${smtpUser}"
smtpPassword="${smtpPassword}" smtpDebug="false" bufferSize="2">
</SMTP>
<!-- appender to send mails asynchronously -->
<Async name="AsyncMailer" >
<appender-ref ref="Mailer"/>
</Async>
</appenders>
<loggers>
<!-- logger to send mail on (at least) info level events -->
<logger name="LogTest" level="info" additivity="true">
<appender-ref ref="AsyncMailer"/>
</logger>
<!-- root logger to see what happens (info level and "above") -->
<root level="info">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
I used this small program to reproduce the problem (LogTest.java):
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
class LogTest
{
private static Logger logger=LogManager.getLogger("LogTest");
public void testlogger()
{
/* --> uncomment to enable first mail
logger.info("test info 1");
*/
logger.fatal("test fatal 1");
/* --> uncomment to enable second mail
logger.info("test info 2");
*/
logger.fatal("test fatal 2");
}
public static void main(String[] args)
{
LogTest app=new LogTest();
app.testlogger();
}
}
If you uncomment the two marked positions everything work like intended: two mails are sent - each containing the fatal-event and the prior info event. Additionally the 4 events are printed to stdout:
test info 1
test fatal 1
test info 2
test fatal 2
Now, if you only activate/uncomment the second position - the second mail (fatal2) is sent as intended (again with the prior info2 event), but even though the first fatal event is printed to stdout the mail is eaten up. The output looks as follows:
test fatal 1
test info 2
test fatal 2
Personally, for me it seems like I got something wrong and mis-configured log4j2 or it might be a bug.
Thanks for your help in advance.
*Jost
Note:
For the tests I used log4j2-beta7 downloaded from the project's website.
The documentation can be found here.
At first glance this looks like a bug. Does it still happen if you remove the LogTest logger and configure your root logger like this?
<root level="info">
<appender-ref ref="Console"/>
<appender-ref ref="AsyncMailer"/>
</root>
FYI, if later you need different log levels on the different appenders you can achieve that like this (no need for a separate logger):
<root level="trace">
<appender-ref ref="A" level="info" />
<appender-ref ref="B" level="debug" />
</root>
I have a class with several methods. I would like each method to output to a different log file. With a logback.xml file like this, it logs ALL logging calls from the class.
<logger name="com.mincom.ellipse.conversion.BatchConverter" level="INFO">
<appender-ref ref="FILE" />
</logger>
How do I get per method logging calls. I'm sure it's very simple, but I cannot seem to see the answer in the doco.
Haven't used logback, but in log4j and others you can setup loggers with any name you like. Using the classes package and name is just a convention. So I'd setup Multiple loggers in your class, something like this:
Logger logA = LogFactory.getLogger("LogA");
Logger logB = LogFactory.getLogger("LogB");
public void methodA() {
logA.debug(...);
}
public void methodB() {
logB.debug(...);
}
And then in your logback setup ...
<logger name="LogA" level="INFO">
<appender-ref ref="FILE-A" />
</logger>
<logger name="LogB" level="INFO">
<appender-ref ref="FILE-B" />
</logger>
Should work. Probably needs some tweaking :-)