getAppender return null from xml - log4j

when I try to read a appender from XML I get return null,
This is my file. xml
<appender name="FileInfoStatistics" class="utils.monitorstatistics.CustonAppenderRollFile">
<param name="file" value="Statistics.log"/>
<param name="MaxFileSize" value="100KB"/>
<layout class="utils.monitorstatistics.CustomAppenderStatistics">
<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
</layout>
</appender>
<logger name="utils.monitorstatistics" additivity="false">
<level value="INFO"/>
<appender-ref ref="FileInfoStatistics"/>
</logger>
and this is the part java where I try to read the appender:
Logger mylogger = Logger.getLogger("utils.monitorstatistics");
Appender app = mylogger.getAppender("FileInfoStatistics");
getAppender return null

Related

Passing log4net ConfigurationFile to a new AppDomain

What I need to do is get hold of the the :
<param name="File" value=".\MyExe.exe.log"/>
value but when I try to access the appender using the following :-
var rootAppender = ((Hierarchy)LogManager.GetRepository())
.Root.Appenders.OfType<RollingFileAppender>()
.FirstOrDefault();
var myFilePath = rootAppender != null ? rootAppender.File : string.Empty;
I find all I get is null. So how do I access my path?
This is my log4net config section :-
<log4net threshold="All">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value=".\MyExe.exe.log"/>
<param name="AppendToFile" value="true"/>
<param name="ImmediateFlush" value="true"/>
<param name="RollingStyle" value="size"/>
<param name="MaxSizeRollBackups" value="5"/>
<param name="MaximumFileSize" value="1MB"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
</layout>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
</appender>
<logger name="MYLOGGER">
<level value="All"/>
<appender-ref ref="RollingLogFileAppender"/>
</logger>
</log4net>
Thanks
The reason you can't find the file name is that you are querying the root logger - but you don't have a root logger defined in your config, and the default root does not have any appenders linked to it.
You would need to run this code instead which uses your defined logger to get the filename:
var loggerAppender = LogManager.GetLogger("MYLOGGER").Logger.Repository
.GetAppenders()
.OfType<RollingFileAppender>()
.FirstOrDefault();
var myFilePath = loggerAppender != null ? loggerAppender.File : string.Empty;

RootLogger to output different levels to different files

I want to have log4j output INFO level to info.log and DEBUG level to debug.log, so that with this piece of code:
logger.info("This is an info message");
logger.debug("This is a debug message");
only the first would go to info.log, while both would go to debug.log.
This doesn't work:
log4j.rootLogger=INFO, R, T
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%r %-5p %c %x - %m%n
log4j.appender.R=org.apache.log4j.FileAppender
log4j.appender.R.File=info.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.R.Threshold = INFO
log4j.appender.T=org.apache.log4j.FileAppender
log4j.appender.T.File=debug.log
log4j.appender.T.layout=org.apache.log4j.PatternLayout
log4j.appender.T.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.T.Threshold = DEBUG
Neither does this:
log4j.rootLogger=INFO, R
log4j.rootLogger=DEBUG, T
log4j.appender.R=org.apache.log4j.FileAppender
log4j.appender.R.File=info.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.R.Threshold = INFO
log4j.appender.T=org.apache.log4j.FileAppender
log4j.appender.T.File=debug.log
log4j.appender.T.layout=org.apache.log4j.PatternLayout
log4j.appender.T.layout.ConversionPattern=%p %t %c - %m%n
log4j.appender.T.Threshold = DEBUG
You can use a filter, org.apache.log4j.varia.LevelMatchFilter. e.g.:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN"
"log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- INFO appender -->
<appender name="INFOFILE" class="org.apache.log4j.FileAppender">
<param name="file" value="info.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern"
value="%d{yyyyMMdd-HHmmss.SSS} %-5p (%c.java:%L).%M - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="levelToMatch" value="INFO" />
</filter>
</appender>
<!-- DEBUG appender -->
<appender name="DEBUGFILE" class="org.apache.log4j.FileAppender">
<param name="file" value="debug.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern"
value="%d{yyyyMMdd-HHmmss.SSS} %-5p (%c.java:%L).%M - %m%n" />
</layout>
<filter class="org.apache.log4j.varia.LevelMatchFilter">
<param name="levelToMatch" value="DEBUG" />
</filter>
</appender>
<!-- root -->
<root>
<priority value="ALL" />
<appender-ref ref="INFOFILE" />
<appender-ref ref="DEBUGFILE" />
</root>
</log4j:configuration>
Put all this in one file in the default package: log4j.xml.

Log4j : How do I lookup the datasource in log4jconf.xml

I am using weblogic for application deployment and I have created a datasource with jndi name "MyDataSource". when I try to use it in my log4j configuration, it is not working
<appender name="myDbAppender" class="org.apache.log4j.jdbc.JDBCAppender">
<param name="jndiName" value="MyDataSource"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="INSERT INTO LOGGING (user_id,
correlation_id, first_name, last_name, event_name, role,
status, access_level, message, logger, loglevel)
VALUES ( '%X{USER_ID}', '%X{CORRELATION_ID}', '%X{FIRST_NAME}',
'%X{LAST_NAME}','%X{EVENT_NAME}','%X{ROLE}','%X{STATUS}','%X
{ACCESS_LEVEL}',
'%m' , '%X{LOGGER}','%p' )"/>
</layout>
</appender>
If you want to use this feature, you need to add the jar file of Apache Extras for Apache log4j and use the class org.apache.log4j.DBAppender. e.g.:
<!-- console -->
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.SimpleLayout" />
</appender>
<!-- db -->
<appender name="DBOUT" class="org.apache.log4j.DBAppender">
<connectionSource class="org.apache.log4j.receivers.db.JNDIConnectionSource">
<param name="jndiLocation" value="java:/comp/env/jdbc/MySQLDS" />
</connectionSource>
</appender>
<!-- root -->
<root>
<priority value="ALL" />
<appender-ref ref="STDOUT" />
<appender-ref ref="DBOUT" />
</root>

log4j isolating certain level from a class

I want to ask something on log4j. I have this config file for log4j on activemq. My problem is that I want to log all INFO level messages from every class I have, but I want to log all DEBUG level messages from "TransportConnection" class to a different file and, at the same time, log all only messages that are greater or equal to WARN level, to the rootLogger.
The problem with this configuration is that I log INFO level messages from "TransportConnection" class in the rootLogger. I want to pass only the WARN and above level to the rootLogger.
I dont want to set a Threshold to the "out" appender, because I want INFO level messages from other classes.
log4j.rootLogger=INFO,out
# Log these warnings
log4j.logger.org.apache.activemq.broker.BrokerRegistry=INFO
log4j.logger.org.apache.activemq.broker.TransportConnection=DEBUG,tc
# Standard logging
log4j.appender.out=org.apache.log4j.RollingFileAppender
log4j.appender.out.file=/var/lib/activemq/log/activemq.log
log4j.appender.out.maxFileSize=10240KB
log4j.appender.out.maxBackupIndex=100
log4j.appender.out.append=true
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
# Transport Connections logging
log4j.appender.tc=org.apache.log4j.RollingFileAppender
log4j.appender.tc.file=/var/lib/activemq/log/tc.log
log4j.appender.tc.maxFileSize=10240KB
log4j.appender.tc.maxBackupIndex=100
log4j.appender.tc.append=true
log4j.appender.tc.layout=org.apache.log4j.PatternLayout
log4j.appender.tc.layout.ConversionPattern=%d [%t] %-5p %-30.30c{1} - %m%n
You could write your own custom filter. I suggest that implementation could be something like that:
public class MinLevelForParticularClassFilter extends Filter {
private boolean acceptOnMatch = false;
private Level level;
private String className;
#Override
public int decide(LoggingEvent event) {
if (this.className != null && this.level != null) {
if (event.getLocationInformation().getClassName().startsWith(className)) {
// this is event for specified class
if (!event.getLevel().isGreaterOrEqual(this.level)) {
// level of event is less than specified level
return Filter.DENY;
}
}
}
if (acceptOnMatch) {
return Filter.ACCEPT;
} else {
return Filter.NEUTRAL;
}
}
public boolean isAcceptOnMatch() { return acceptOnMatch; }
public void setAcceptOnMatch(boolean acceptOnMatch) { this.acceptOnMatch = acceptOnMatch; }
public Level getLevel() { return level; }
public void setLevel(Level level) { this.level = level; }
public String getClassName() { return className; }
public void setClassName(String className) { this.className = className; }
}
Note that if you change "className" variable to "packageName" variable, the implementation will be the same for filter by particular package.
The log4j configuration in XML (since filters are unsupported by configuration in property files):
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="out" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="activemq.log" />
<param name="MaxFileSize" value="10240KB" />
<param name="MaxBackupIndex" value="100" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%-15.15t] %-5p %-30.30c{1} - %m%n"/>
</layout>
<!-- Apply filter to appender that's destined for root logger -->
<filter class="com.foo.log4j.filters.MinLevelForParticularClassFilter">
<param name="Level" value="WARN" />
<param name="ClassName" value="org.apache.activemq.broker.TransportConnection" />
<param name="AcceptOnMatch" value="true" />
</filter>
</appender>
<appender name="tc" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="tc.log" />
<param name="MaxFileSize" value="10240KB" />
<param name="MaxBackupIndex" value="100" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %-30.30c{1} - %m%n"/>
</layout>
</appender>
<logger name="org.apache.activemq.broker.BrokerRegistry">
<level value="INFO" />
</logger>
<logger name="org.apache.activemq.broker.TransportConnection">
<level value="DEBUG" />
<appender-ref ref="tc" />
</logger>
<root>
<priority value ="INFO" />
<appender-ref ref="out" />
</root>
</log4j:configuration>

Can the log file path be changed on the fly (reguarly)

I've been experimenting with log4net as it appears that it does not support a particular logging feature my project needs. In short, I want to be able to control the log file path in code. This path will change constantly. The specific use case is a set of file system watchers, and a separate log file is required per instance.
Can this be done?
I want to be able to specify a variable that controls the logging destination in code.
For example:
var log4NetLogger1 = new Log4NetLogger("LogFileAppender1");
log4NetLogger1.InformationEvent("Log message 1");
var log4NetLogger2 = new Log4NetLogger("LogFileAppender2");
log4NetLogger2.InformationEvent("Log message 2");
In the above example, I am passing a string to the log4net wrapper, that matches an appender name in configuration. The idea is that the log4net wrapper changes the logging target by modifying the appender in use via GetLogger.
_log = log4net.LogManager.GetLogger(appenderName);
The result is that two different log files are created, but the log message is written to both. It appears that some aspect of the log4net configuration is global, and I am not able to change log path on the fly in this way.
Configuration file:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<appender-ref ref="LogFileAppender1"/>
<appender-ref ref="LogFileAppender2"/>
</root>
<appender name="LogFileAppender1" type="log4net.Appender.RollingFileAppender">
<threshold value="INFO"/>
<param name="File" value="C:\Tmp\EDP_TEST\LOG_DESTINATION\TestLog1.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
<appender name="LogFileAppender2" type="log4net.Appender.RollingFileAppender">
<threshold value="INFO"/>
<param name="File" value="C:\Tmp\EDP_TEST\LOG_DESTINATION\TestLog2.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
</log4net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
Full wrapper class:
using System;
using log4net;
namespace Log4NetRunner
{
public enum LoggingLevel
{
Information,
Warning,
Error
}
public class Log4NetLogger
{
private readonly ILog _log;
public Log4NetLogger(Type type)
{
if (_log == null)
{
_log = log4net.LogManager.GetLogger(type);
}
}
public Log4NetLogger(string appenderName)
{
if (_log == null)
{
_log = log4net.LogManager.GetLogger(appenderName);
}
}
public void FatalErrorEvent(string messageText)
{
SendLog(messageText, LoggingLevel.Error);
}
public void WarningEvent(string messageText)
{
SendLog(messageText, LoggingLevel.Warning);
}
public void InformationEvent(string messageText)
{
SendLog(messageText, LoggingLevel.Information);
}
private void SendLog(string messageText, LoggingLevel logLevel)
{
ILog logger = _log;
switch (logLevel)
{
case LoggingLevel.Error:
logger.Error(messageText);
break;
case LoggingLevel.Warning:
logger.Warn(messageText);
break;
case LoggingLevel.Information:
logger.Info(messageText);
break;
default:
logger.Error("Unknown Logging level: " + messageText);
break;
}
}
}
}
Yes it can, you just need to use the loggers section of the config.
http://www.beefycode.com/post/Log4Net-Tutorial-pt-5-Using-Logger-Objects.aspx
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<root>
<!--<appender-ref ref="LogFileAppender1"/>
<appender-ref ref="LogFileAppender2"/>-->
</root>
<logger name="Logger1">
<level value="ALL" />
<appender-ref ref="LogFileAppender1" />
</logger>
<logger name="Logger2">
<level value="ALL" />
<appender-ref ref="LogFileAppender2" />
</logger>
<appender name="LogFileAppender1" type="log4net.Appender.RollingFileAppender">
<threshold value="INFO"/>
<param name="File" value="C:\Tmp\EDP_TEST\LOG_DESTINATION\TestLog1.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
<appender name="LogFileAppender2" type="log4net.Appender.RollingFileAppender">
<threshold value="INFO"/>
<param name="File" value="C:\Tmp\EDP_TEST\LOG_DESTINATION\TestLog2.txt"/>
<param name="AppendToFile" value="true"/>
<rollingStyle value="Size"/>
<maxSizeRollBackups value="10"/>
<maximumFileSize value="10MB"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n"/>
</layout>
</appender>
</log4net>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

Resources