i want to log messages from multiple classes. the problem is that currently i can log all messages into single file.i want to do this class A should log its messages into ALog.txt and class B should log its messages into BLog.txt
please tell me the sample config file for these settings
In addition to the root logger, you can configure individual loggers with their own appenders. Assuming you are calling your loggers ClassA and ClassB you can do:
<root>
<appender-ref ref="CommonAppender" />
</root>
<logger name="ClassA">
<appender-ref ref="AppenderA" />
</logger>
<logger name="ClassB">
<appender-ref ref="AppenderB" />
</logger>
This is further described here.
Related
I have JBoss 6.1 and using jboss-logging.xml
I would like to set different log level for one package, and one his subpackage, example:
- com.foo.bar (in DEBUG)
- com.foo.bar.subpack.subsubpack (in INFO)
I have tried with this:
<logger category="com.foo.bar">
<level name="DEBUG"/>
</logger>
<logger category="com.foo.bar.subpack">
<level name="INFO"/>
</logger>
But in the log I continue to see all the debug message from the class in the subpack
I find the problem: using the constructor Logger.getLogger("PIPPO"), in the xml the category is "PIPPO":
<logger category="PIPPO">
<level name="INFO"/>
</logger>
This tag control the level message of the class
I guess the messages are readen twice by the loggers. Disable additivity :
<logger category="com.foo.bar" additivity="false">
<level name="DEBUG"/>
</logger>
<logger category="com.foo.bar.subpack">
<level name="INFO"/>
</logger>
Edit :
Otherwise, check the name by whitch your are instanciating your logger.
According to https://logging.apache.org/log4j/1.2/manual.html#Logger_hierarchy, if you write Logger.getLogger("PIPPO"), and if the logger with name "PIPPO" does not exist in your configuration(s) file(s), the RootLogger will be instanciated by default.
A logger is said to be an ancestor of another logger if its name followed by a dot is a prefix of the descendant logger name. A logger is said to be a parent of a child logger if there are no ancestors between itself and the descendant logger.
To apply your configuration, you have to instanciate the logger by calling it with a name like "com.foo.bar.subpack.YourClass".
Following configuration logs to Eventlog and it works for SmtpSender. But the filelogger does not log, it creates the log file in the path, but does not do any debug logs.
<root>
<level value="DEBUG" />
<appender-ref ref="EventLogAppender" />
</root>
<logger additivity="false" name="SmtpLogger">
<level value="FATAL"/>
<appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
But when I change the root logger to RollingFileAppender it logs to file,
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
<logger additivity="false" name="SmtpLogger">
<level value="FATAL"/>
<appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
Any idea why this happens? How can I get file logger working in this scenario.
You did not post the appender confuriguration, however the easiest way to figure out what goes wrong is enable internal debugging. This will tell you what goes wrong in the rolling file appender:
There are 2 different ways to enable internal debugging in log4net.
These are listed below. The preferred method is to specify the
log4net.Internal.Debug option in the application's config file.
• Internal debugging can also be enabled by setting a value in the
application's configuration file (not the log4net configuration file,
unless the log4net config data is embedded in the application's config
file). The log4net.Internal.Debug application setting must be set to
the value true. For example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
This setting is read immediately on startup an will cause all internal
debugging messages to be emitted.
• To enable log4net's internal debug programmatically you need to set
the log4net.Util.LogLog.InternalDebugging property to true. Obviously
the sooner this is set the more debug will be produced.
Internal debugging messages are written to the console and to the
System.Diagnostics.Trace system. If the application does not have a
console the messages logged there will be lost. Note that an
application can redirect the console stream by setting the
System.Console.Out. The Trace system will by default send the message
to an attached debugger (where the messages will appear in the output
window). If the process does not have a debugger attached then the
messages are sent to the system debugger. A utility like DebugView
from http://www.sysinternals.com may be used to capture these
messages.
As log4net internal debug messages are written to the
System.Diagnostics.Trace system it is possible to redirect those
messages to a local file. You can define a trace listener by adding
the following to your application's .config file:
<configuration>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
...
</configuration>
Make sure that the process running your application has permission to write to this file.
log4net faq
I managed to get the logging work, in fact I did this as an experiment and it worked.
Added RollingFileAppender to the root element,
<root>
<level value="DEBUG" />
<appender-ref ref="EventLogAppender" />
<appender-ref ref="RollingFileAppender"/>
</root>
<logger additivity="false" name="SmtpLogger">
<level value="FATAL"/>
<appender-ref ref="SmtpAppender" />
</logger>
<logger name="RollingFileAppender">
<level value="DEBUG"/>
<appender-ref ref="RollingFileAppender"/>
</logger>
I've set up log4net to potentially log to three locations: console, rolling file, and a database. I need to be able to turn on and off these three separate locations individually, so I've created a set of loggers, one for each combination. For example...
<logger name="Logfile">
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</logger>
<logger name="Console">
<level value="ALL" />
<appender-ref ref="ConsoleAppender" />
</logger>
<logger name="LogfileConsole">
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
<appender-ref ref="ConsoleAppender" />
</logger>
My root logging level is set to OFF with no appenders used so that the default is no logging whatsoever. HOWEVER... when I run one of the projects that has logging set to the root, meaning it is turned off, a log file still gets created. The file remains empty and no logging is performed, but if I'm using a logger without the RollingFileAppender in it, why is it creating the log file?
I thought it might be because the root was totally empty, so I made a logger called "None" and set the level to OFF and gave it the ConsoleAppender just for appearances sake, and it still created the file.
It's not interfering with the running of the project or impacting performance, but I'd prefer my log folder not end up cluttered with empty log files for apps that shouldn't be logging. I CANNOT figure out why it's creating a file when the current logger does not use file appender. Help?
I cannot get nservicebus to use a named log4net logger. It seems to only use what is defined in the <root> element.
This works fine
NServiceBus.SetLoggingLibrary.Log4Net(log4net.Config.XmlConfigurator.Configure);
<root>
<level value="ALL"/>
<appender-ref ref="NServiceBusAppender"/>
</root>
But i really like it to use a named definition.
<logger name="NServiceBusLogger">
<appender-ref ref="NServiceBusAppender"/>
</logger>
Is this possible?
On their website they are only showing a sample with the <root> behaviour.
http://docs.particular.net/nservicebus/logging/
All loggers in NServicebus begins with the name "NServiceBus". Have you tried this:
<logger name="NServiceBus">
<appender-ref ref="NServiceBusAppender"/>
</logger>
I guess you need specify the level on the named logger:
<logger name="NServiceBusLogger">
<level value="ALL"/>
<appender-ref ref="NServiceBusAppender"/>
</logger>
(I assume now that the name of the logger is correct.)
The app I'm working on uses a library that generates a lot of INFO level messages I don't want to log. But the rest of the app and libraries produce INFO level messages I need to log.
How can I setup log4net so that all INFO messages are logged except for one logger that need to log only at WARN or above ? All messages need to be logged in the same files.
Julien
<root>
<level value="INFO" />
<appender-ref ref="someappender" />
</root>
<logger name="AnotherLogger">
<level value="WARN" />
</logger>