How to configure log4j to show only my
log.debug("test log");
messages in console without other system generated information?
It's very disturbing when in small app your console is messed with tons of useless ( at least for me) information like
DEBUG org.springframework.beans.CachedIntrospectionResults: Getting BeanInfo for class [org.thymeleaf.spring4.view.ThymeleafView]
my log4j.properties file:
log4j.rootLogger=DEBUG, stdout, 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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
You can use the LoggerMatchFilter and DenyAllFilter to restrict your logging appender to messages coming from your code only.
LoggerMatchFilter filter = new LoggerMatchFilter();
filter.setLoggerToMatch("Your.Root.Namespace");
filter.setAcceptOnMatch(true);
log4j.appender.stdout.addFilter(filter); // Match your messages only.
log4j.appender.stdout.addFilter(new DenyAllFilter()); // Don't match anything else.
Is your application using log4j or yet to configure??
log4j configure steps
If already log4j is in use, change the logger level to ERROR
Check whether your applications is using any xml configuration file for log4j or properties file so you can change in it.
logger level configuration steps.
Related
I am completely new to Spring MVC and log4j.
In a project that I referred, log4j outputs all the mapped URLs in the console at server startup.
But when I tried, it is not displaying anything about the controller methods.
I tried to change the level from INFO to DEBUG but that's too deep and not what I needed.
I wrote a single API for example purpose.
sample api
And these are my log4j properties
log4j.rootLogger=INFO, stdout, file
#log4j.rootLogger=DEBUG, stdout, file
# Direct log messages to stdout
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{yyyy-MM-dd
HH:mm:ss} %-5p %c{1}:%L - %m%n
### direct messages to file or.log ###
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${user.home}/Documents/rough/
hermesTmp/hermes.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd MMM yyyy
HH:mm:ss,SSS} %5p %c{1} - %m%n
log4j.appender.file.append=true
log4j.appender.file.MaxFileSize=10000KB
log4j.appender.file.MaxBackupIndex=10
This is my output
doesnt show mapped url
I need the API to be listed as shown in the project I referred
Working code's output
Any suggestions...?
Thanks in advance..
The debug() method you are using on the log object in your controller will log on DEBUG level. So you can either change the method you invoke to info():
log.info("Everything will work this way")
Or you can change the level in log4j configuration to:
log4j.rootLogger=DEBUG, stdout, file
You can check out the documentation here
I have included a custom jar for one of my jasper reports and it is working fine.Now for debugging purposes I want to implement log4j/slf4j in my classes in this custom jar so that when report is previewed in Jaspersodt Studio or run on JasperSoft server i can see the logging statements in a file.
Right now I have added all configuration but loggers are not printing in the file.My guess is that log4j is not getting triggered.
1)Is there any more setting/configuration is required?
2) I checked Jaspersoft server has default implementation for log4j and it logs but only various queries.Is there any particular setting/logger to set so that it includes custom jar loggers as well?
Below log4j.properties is put into resources folder of my maven project(custom jar)
log4j.rootLogger=DEBUG, stdout, 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{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\log4j-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
EDIT:
Below is Java code.This is exported in a jar and added in classpath in my jasper project.When I preview my report chart is created and this customizer is excuted but I do not see any log statements in the file I have given in log4j.properties above :
public class BarChartDemo {
static Logger LOGGER = Logger.getLogger(BarChartDemo.class);
public void customize(JFreeChart chart, JRChart jasperChart) {
LOGGER.debug("debug");
LOGGER.info("info");
LOGGER.warn("warn");
LOGGER.error("error");
?/customizer code
}
}
has anyone tried to use a log4j.xml reference within a WinRun4j service configuration. here is a copy of my service.ini file. I have tried many configuration combinations. this is just my latest attempt
service.class=org.boris.winrun4j.MainService
service.id=SimpleBacnetIpDataTransfer
service.name=Simple Backnet IP DataTransfer Service
service.description=This is the service for the Simple Backnet IP DataTransfer.
service.startup=auto
classpath.1=C:\Inbox\DataTransferClient-1.0-SNAPSHOT-jar-with-dependencies.jar
classpath.2=WinRun4J.jar
classpath.3=C:\Inbox\log4j-1.2.16.jar
arg.1=C:\Inbox\DataTransferClient.xml
log=C:\WinRun4J-Service\SimpleBacnetIpDataTransfer\NBP-DT-service.log
log.overwrite=true
log.roll.size=10MB
[MainService]
class=com.shiftenergy.ws.App
vmarg.1=-Xdebug
vmarg.2=-Xnoagent
vmarg.3=-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n
vmarg.4=-Dlog4j.configuration=file:C:\Inbox\log4j.xml
within the log4j.xml file, there is reference to a log file for when the application runs. if I run the java -jar -Dlog4j.configuration=file:C:\Inbox\log4j.xml ...., the log file is created accordingly. if I register my service and start the service, the log file does not get created.
has anyone had success using the -D log4j configuration, using winrun4j?
thanks
I think that you provided the vmarg.4 parameter incorrectly. In your case it has to be like:
vmarg.4=-Dlog4j.configurationFile=[Path for log4j.xml]
I am also using the same and in my case, it is working perfectly fine. Please see below example:
vmarg.1=-Dlog4j.configurationFile=.\log4j2.xml
Have you tried setting the path in your code instead:
System.setProperty("log4j.configurationFile", "config/log4j.xml");
I'm using a relative path to a folder named config that contains log4j.xml. An absolute path is not recommended, but may work as well.
Just be sure to set this before making any calls to log4j, including any log4j config settings or static method calls!
System.setProperty("log4j.configurationFile", "config/log4j.xml");
final Logger log = Logger.getLogger(Main.class);
log.info("Starting up");
I didn't specify the log4j path in the ini file, only placed log4j.xml file at the same place the jar was placed.
Also without specify the
System.setProperty("log4j.configurationFile", "config/log4j.xml");
In the Java project it was stored in (src/main/resources) and will be included in the jar, but it will not be that one used if placed outside the jar.
I am using log4qt with RollingFileAppender in my QT application. Everytime I restart my application logs are not appended to the log file, the file is simply overwritten.
Below is my log4qt.properties file.
log4j.rootLogger = DEBUG, File
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/log.out
log4j.appender.File.MaxFileSize=3072KB
log4j.appender.File.MaxBackupIndex=3
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=%d [%t] [%p] %m%n
I tried to add the append property log4j.appender.File.Append=true , but i got error Property 'append' does not exist.
By exploring the code, I myself figured out the property that needs to be configured for appending the logs to the file.
The correct property is:
log4j.appender.File.AppendFile=true
My problem is that i'm trying to set up log4j to log into a file from my jar. This jar is used by an application which already uses log4j. My jar is made as maven package shipped with log4j. I'm trying to initialize log4j from property file as such:
"log4j.properties"
log4j.appender.FileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.FileAppender.File=${user.home}/.myproject/myproject.log
log4j.appender.FileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.FileAppender.layout.ConversionPattern=%d{yy-MM-dd HH:MM:ss,SSS} [%t] %-5p %c %x - %m%n
log4j.appender.FileAppender.MaxFileSize=5MB
#myproject is full package name
log4j.logger.myproject=,FileAppender
I've included this property file into resources of my jar. I'm loading it with code:
try {
InputStream inputStream = this.getClass().getClassLoader()
.getResourceAsStream("log4j.properties");
Properties properties = new Properties();
properties.load(inputStream);
inputStream.close();
PropertyConfigurator.configure(properties);
} catch (NullPointerException e) {
BasicConfigurator.configure();
throw new MyprojectException("log4j.properties not found", e);
} catch (IOException e) {
throw new MyprojectException("log4j.properties could not be loaded", e);
}
This code is working flawlessly when my jar is not used by an application which uses log4j. But it is, i get an empty log file or for some cases in some applications, no log file is created at all.
I've tried to add an appender manually, not with propertyconfigurator like this:
static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Myproject.class);
SimpleLayout layout = new SimpleLayout();
FileAppender appender = new FileAppender(layout,"C:/out.log",true);
logger.addAppender(appender);
logger.setLevel(Level.DEBUG);
This seems to be working fine, but i'd really want to configure log4j from properties file instead.
I've read lots of threads about how log4j handles multiple instances. I've found out that i cannot alter log4j configuration loaded by properties once it's done by one application. Some have suggested i shall ship my own log4j jar and config and configure with propertyconfigurator, that's what i've tried and as you can see it didn't work. I'm curious why my second example is working and the first is not. How can i solve this problem?
notes:
I do not want to alter the log4jconfigs of the applications that use my jar file.
I've also found out that there is -Dlog4j.configuration system property that could be used, but my jar is not an executeable jar file, it gets loaded by Class.forName
Any help is appreciated,
Thanks in advance