In Log4J, why %C in ConversionPattern prints '?' (question mark) with AsyncAppender? - log4j

I have a trouble when using %C in ConversionPattern with AsyncAppender.
My Lo4J configuration is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss,SSS} %C{1} - %m%n" />
</layout>
</appender>
<appender name="async_console" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="1000" />
<appender-ref ref="console" />
</appender>
<root>
<level value="debug" />
<!--
<appender-ref ref="console" />
-->
<appender-ref ref="async_console" />
</root>
</log4j:configuration>
And my test code is:
#Test
public void testAsync() {
DOMConfigurator
.configure("src/test/resources/learningtest/log4j/log4j_test_async.xml");
Logger log = Logger.getLogger(getClass());
log.debug("Hello, world!");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
The result of the test code is:
2012/03/15 11:51:22,570 ? - Hello, world!
Without AsynAppender, it works fine:
2012/03/15 11:51:06,002 Log4jTest - Hello, world!
With %c (category), it works fine, too.
What am I missing?
Please let me know.
Thanks in advance :-)
Reference:
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

When using "%C" or "%M", log4J uses Throwable.getStackTrace to get the stackTrace and use this information to get the caller class and method.
The problem is that when using an AsyncAppender, the Throwable is created in another thread and the stackTrace does not contain the caller method.

Related

log4cxx asyncappender flush all logs before exiting

I am using Asynappender and DOMConfigurator to load the xml file.
I am losing half of the logs. My application exits but only few logs appears in log file.
I have found that calling close() method of AsyncAppender class processes pending events before exiting.
Does this means that it flushes all the logs to log file? And if yes then, as I don't have that object as I am simply loading file using DOMConfigurator::configure().
How can I retrieve the Asynappender object to call close()?
Is there any other way to flush the logs before exiting? Using some configuration in xml file?
Below is my code for reference:
#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>
#include<iostream>
using namespace log4cxx;
using namespace log4cxx::xml;
using namespace log4cxx::helpers;
LoggerPtr loggerMyMain(Logger::getLogger( "main"));
int main(int args, char **argv)
{
DOMConfigurator::configure("asynclog4cxxconfig.xml");
LOG4CXX_TRACE(perf, "this is a performance message!!!");
LOG4CXX_DEBUG(loggerMyMain, "this is a debug message.");
LOG4CXX_WARN (loggerMyMain, "this is a warn message, not too bad.");
LOG4CXX_ERROR(loggerMyMain, "this is a error message, something serious is happening.");
LOG4CXX_FATAL(loggerMyMain, "this is a fatal message!!!");
return 0;
}
Below is the XML file:
<?xml version="1.0" encoding="UTF-8" ?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="appxNormalAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="logfile" />
<param name="MaxFileSize" value="1000KB" />
<param name="MaxBackupIndex" value="3" />
<param name="append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%X{process} %d %-5p %C{2} (%F:%L) - %m%n" />
</layout>
</appender>
<appender name="async_appxNormalAppender" class="org.apache.log4cxx.AsyncAppender">
<appender-ref ref="appxNormalAppender"/>
</appender>
<root>
<priority value="debug" />
<appender-ref ref="async_appxNormalAppender"/>
</root>
</log4j:configuration>
The problem got solved, posting it here if it helps someone.
Added the below piece of code at the end.
LoggerPtr root_logger = Logger::getRootLogger();
AppenderPtr app_ptr = root_logger->getAppender("async_appxNormalAppender");
if(app_ptr != NULL)
app_ptr->close();

RemotingAppender problems. Conn created but nothing appended

I have been learning log4net and wish to use the remoting appender to log messages onto a server sometime in the future. To do this, I first tried creating a local .Net remoting server and appending to it. It seems to me that the server has been created but I cannot receive these messages. (To check this, I try accessing the server by entering localhost:portnumber in my browser, before and after running my program. It fails before and accepts the connection later. Any better way to debug this?)
Anyway, here is the code. I would appreciate any help.
PS: I can see the File & Console appenders work.
Client code
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.onfig.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="Console" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="DEBUG"/>
<foreColor value="Red, HighIntensity"/>
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%class %date [%level] - %message%newline"/>
</layout>
</appender>
<appender name="File" type="log4net.Appender.FileAppender">
<file value="logfile.txt"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%class %date [%level] - %message%newline"/>
</layout>
</appender>
<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender">
<sink value="tcp://localhost:8086/RemoteLogger"/>
<lossy value="false"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%class %date [%level] - %message%newline"/>
</layout>
<bufferSize value="1"/>
<onlyFixPartialEventData value="true"/>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="Console"/>
<appender-ref ref="RemotingAppender"/>
<appender-ref ref="File"/>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
Server code Appconfig
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application name="RemoteLogger">
<channels>
<channel name="logging sink" ref="tcp server" port="8086"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>
Server Code
namespace RemoteAPP
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Listening");
var _sink = new RemoteSink();
_sink.EventsReached += (s,a)=> AddLog(a.LoggingEvents);
RemotingConfiguration.Configure("RemoteAPP.exe.config", false);
//RemotingConfiguration.RegisterWellKnownServiceType(new WellKnownServiceTypeEntry(typeof(RemoteSink), "RemoteLogger", WellKnownObjectMode.SingleCall));
RemotingServices.Marshal(_sink, "RemoteLogger");
Console.ReadLine();
}
private static void AddLog(IEnumerable<LoggingEvent> enumerable)
{
var Logevents = enumerable.ToList();
foreach(var logevent in Logevents)
{
Console.WriteLine(logevent);
}
}
}
public class RemoteSink:MarshalByRefObject,RemotingAppender.IRemoteLoggingSink
{
public class LoggingArgs:EventArgs
{
public IEnumerable<LoggingEvent> LoggingEvents;
}
public EventHandler<LoggingArgs> EventsReached;
void RemotingAppender.IRemoteLoggingSink.LogEvents(LoggingEvent[] events)
{
var ev = EventsReached;
if(ev==null)
{
ev.Invoke(this, new LoggingArgs{LoggingEvents = events});
}
}
}
}
Client Code
class Program
{
static void Main(string[] args)
{
Thread.Sleep(10000);
log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
for (int i = 0; i < 1000;i++ )
{
log.Info("Hello world");
log.Debug("This is Debug");
log.Warn("This is Warn");
}
Well,
I found the answer. The code above is correct. Turns out there was some incompatibility between the client and server side of Log4net. Although I installed both from the same source (Nuget), something changed between the two references. After setting log4net's internal debugger property to true, I stumbled across this log message.
log4net:ERROR [RemotingAppender] ErrorCode: GenericFailure. Failed in SendBufferCallback
Googling it led me to here and this is where I suspected something was amiss. Long story short, removed all references to log4net in RemoteAPP and reinstalled via Nuget.
It works now. If someone knows how this occurred, please continue the discussion.

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