I'm using slf4j + log4j with a library of mine.
I am using it in a quick application where I don't really care about configuring the logging... or at least I didn't think I cared.
If I don't configure log4j, I get the standard warning message
log4j:WARN No appenders could be found for logger com.xyz.abcdbabble
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
which is fine.
If I then do
logger.debug("Blah blah blah")
then the logger doesn't print anything out. Great!
But if I do
if (logger.isDebugEnabled())
{
System.out.println("print some complex stuff:");
print_some_complex_stuff();
}
then the stuff in brackets gets executed. What gives?
I'm looking for a way to determine whether to print out something that should have an equivalent enabling to logger.debug(), even if log4j isn't configured in the end application. How can I do this?
Instead of trying to figuring out the logic of the log4j default configuration, I would put in a config file (log4j.properties), like:
log4j.rootLogger=debug, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
At least this puts you into control.
Instead of bundling with log4j then bundle with the "simple" backend from the slf4j distribution. Then things work as you want them to, and the users cannot tinker.
Related
We are using log4net for our application logging in azure.
We configured it according to this document , the logs are stored in
d:/home/LogFiles/Application/Log4netfile-[%processid].txt
we can download the files from https://xxxx.scm.azurewebsites.net/api/dump and I can also see them in Log stream, there it looks like this
2022-02-06T18:22:57 PID[29616] Verbose log4net: Adding appender named [FileAppender] to logger [root].
2022-02-06T18:22:57 PID[29616] Verbose log4net: Hierarchy Threshold []
2022-02-06T18:22:58 PID[29616] Verbose LOG4NET Fatal LOG4NET LOG4NET
2022-02-06T18:22:58 PID[29616] Verbose LOG4NET Debug Message Message
Line items like:
log4net: Adding appender named
are created by log4net during internal setup (using debug=true), and those show up in our AppServiceAppLogs, however, items which are genereted by the logger using:
Logger.Fatal("LOG4NET Fatal LOG4NET LOG4NET");
are visible in stream and files but they are not showing up in AppServiceLogs.
I tried multiple different appenders the only one which shows up in Log-Streams and in files is FileAppender (or RollingFileAppender), all others are not showing in LogStreams or (as expected) in the files.
Based on this https://learn.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#send-logs-to-azure-monitor I assume that everything writen to the files should be than sent to azure-monitor - but in our case this is not hapening. Could you provide some reference about implementation / code how the sending of the files is done, and which process does that ?
I am wandering is there anything which we could configure differently to get the log-items to end up in AppServiceLogs? Or should we use some other appender?
I am aware of the issue in AspNetTraceAppender and I am trying to avoid implementation of new appender.
There are a few things you can look into:
Call log4net configure before writing to your log file (only once is enough):
log4net.Config.XmlConfigurator();
or
log4net.Config.XmlConfigurator.Configure();
and then you can add flushing to your configuration:
<appender name="AzureTraceAppender" type="log4net.Appender.TraceAppender">
<param name="ImmediateFlush" value="true" />
<layout type="log4net.Layout.PatternLayout">
<!-- can be any pattern you like -->
<conversionPattern value="%logger - %message" />
</layout>
</appender>
Immediately, this will flush the message.
In Global.asax, To make log4net read your configuration.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
// Initialize log4net.
log4net.Config.XmlConfigurator.Configure();
}
Make sure Azure Diagnostics has configured to all information needed for debugging.
After that, you can enable internal log4net debugging. Refer internal debugging on this log4net faq page. Standard it should log to your listener you have configured. To the trace element Add the autoflush="true" option . Or find directory on the worker role you can write to and access to read your logs.
Currently on our project we are seeing some DEBUG statements printed on our tomcat logs such as this:
12:09:00.824 [localhost-startStop-1] DEBUG n.j.w.r.h.b.AbstractResourceBundleHandler - Storing a generated bundle with an id of:/script/lib/itegration.dataTables.bootstrap.js 12:09:00.850 [localhost-startStop-1] DEBUG n.j.w.r.h.b.AbstractResourceBundleHandler - Storing a generated bundle with an id of:/script/lib/itegration.dataTables.bootstrap.js
We have determined that the debug statements that are being logged are caused because the condition LOGGER.isDebugEnabled() in the JAWR AbstractResourceBundleHandler.java class is always true. The snip of the code that logs is as follows:
if (LOGGER.isDebugEnabled()) {
String msg = "Storing a generated "
+ (gzipFile ? "and gzipped" : "")
+ " bundle with an id of:" + bundleName;
LOGGER.debug(msg);
}
As you can see there is a LOGGER.isDebugEnabled() which is always true. Is there a way to configure the logging level for this package/class/jar? We are currently using log4j as our logging framework and have tried configuring this package to log only INFO level but that has not worked. We also have the jawr.debug.on property set to false (jawr.debug.on=false)
I understand that JAWR is using slf4j for their logger and there is a way to use the slf4j bridge to log4j which we have configured, we just cannot get the logging level configuation for this package to work.
Does anyone know a way to set the LOGGER.isDebugEnabled() set to false so that these DEBUG log statements do not appear in our tomcat logs?
We are using log4j.xml not a properties file and would like to keep using the .xml configuration if possible.
Thank you
Adding this to our log4j.xml configuration file
<category name="net.jawr">
<priority value="INFO" />
</category>
works fine.
How do we know which is the right Appender to use between RollingFileAppender and FileAppender?
You may want to read the Apache Log4j 1.2.17 API:
org.apache.log4j.FileAppender
FileAppender appends log events to a file.
org.apache.log4j.RollingFileAppender
RollingFileAppender extends FileAppender to backup the log files when they reach a certain size.
I use Robot Framework with custom keywords implemented in java libraries. Messages written directly to System.out from my java classes are visible in the robot output - as promised in documentation. However, since the keyword implementations are reusable components, independent from robot framework, I wanted to have more flexible logging there and not using System.out directly. I thought if I redirect my log output to System.out (with log4j's ConsoleAppender), the messages will be visible in robot output. Unfortunately it does not work.
My log4j properties file:
log4j.appender.Stdout=org.apache.log4j.ConsoleAppender
log4j.appender.Stdout.Target=System.out
log4j.appender.Stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.Stdout.layout.conversionPattern=%d %-5p [%t] %F:%L %m%n
log4j.appender.Stdout.ImmediateFlush=true
log4j.appender.FA=org.apache.log4j.FileAppender
log4j.appender.FA.File=mylog.log
log4j.appender.FA.layout=org.apache.log4j.PatternLayout
log4j.appender.FA.layout.ConversionPattern=%d %-5p [%t] %F:%L %m%n
log4j.rootLogger=INFO,Stdout,FA
The file appender works fine, the log file is created and contains all log messages, but the same messages are not visible either on console or in robot output reports. When I run my components without robot framework, the same config works for console as well.
Do you have an ideas what is wrong with the above configuration? Or any other suggestions to have robot logs while avoiding the direct usage of System.out from my java classes?
By default, the ConsoleAppender uses a saved reference to System.out or System.err. To override this behavior, there is a follow property you must set to true.
Try adding this to your log4j.properties file:
log4j.appender.Stdout.follow=true
WARNING: JSF1064: Unable to find or serve resource, masthead/pic.jpg, from library, img.
Aug 3, 2013 10:18:16 AM com.sun.faces.application.resource.ResourceHandlerImpl logMissingResource
WARNING:
ClientAbortException: java.net.SocketException: Connection reset
How can I hide these warning from the logs?
I'm using logback.
Those logs are not logged via Logback. They are logged via Java SE's builtin java.util.logging interfaces. JULI loggers are configurable via JRE/lib/logging.properties where JRE is the folder holding the Java Runtime Environment. In your specific case, you just need to add the following line to the end of file in order to bring it one level up so that warnings are suppressed.
javax.enterprise.resource.webcontainer.jsf.application.level = SEVERE
See also:
JSF2 logs with tomcat
To manage messages from a certain package you must define an specific logger, and set the level higher than the messages you want to hide.
As an example, to avoid the WARNING messages from com.sun.faces and its sub packeges, you must include the next logger definition at the LogBack configuration file:
<logger name="com.sun.faces" level="ERROR" />
Reference: http://logback.qos.ch/manual/configuration.html
Anyway, you shuold check why your JSF engine is showing these messages, and fix it. Perhaps your web.xml lacks some servlet mapping for masthead/*?