log with log4j from jsf web apllication - jsf

i have been trying to log to a text file with log4j from a simple jsf web application, but no success. Every time i open the file at location, nothing was written to it. I am deploying with glassfish. Below is my log4j.properties configuration
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{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.FILE = org.apache.log4j.RollingFileAppender
log4j.appender.FILE.maxFileSize = 100kb
log4j.appender.FILE.maxBackupIndex = 2
log4j.appender.FILE.File = C:\temp\mylog.txt
log4j.appender.FILE.Threshold = debug
log4j.appender.FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n
i realise that the file mylog.txt gets created in ....\domains\domain1 directory of the glassfish application server.How do i get it to direct to C:\temp\mylog.txt

Got it fixed. Actually my line seperator for file location was in the wrong direction "\" backslash and log4j expected "/" a forward slash line seperator - probably because it expects a url pattern file location address.
So i just changed C:\temp\mylog.txt to C:/temp/mylog.txt and voila! This also applies to a any other file location you want log4j to target

Related

File name is generating as "Application.log.1" on reaching maxFileSize

below is Log4J.properties configuration ( Using jar "log4j-1.2.17.jar")
log4j.logger.devpinoyLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=1KB
log4j.appender.dest1.maxBackupIndex=3
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c%m%n
log4j.appender.dest1.File=log\\Application.log
log4j.appender.dest1.Append=false
but once maxFileSize is reached backup log file is generated as Application.log.1
I want file to be generated as Application1.log
I got the solution using RollingFileAppender.
log4j.logger.devpinoyLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.dest1.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.dest1.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.dest1.triggeringPolicy.MaxFileSize=100
log4j.appender.dest1.rollingPolicy.FileNamePattern=C:\\TRB Workspace\\TRBAutomationFramework\\log\\Application-%i.log
log4j.appender.dest1.rollingPolicy.ActiveFileName=C:\\TRB Workspace\\TRBAutomationFramework\\log\\Application.log
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %c %m%n
log4j.appender.dest1.Append=false
Also required "apache-log4j-extras-1.2.17.jar"
I did not test, but as explained by Koning, you may try to add this to your properties:
log4j.appender.dest1.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.dest1.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.dest1.rollingPolicy.FileNamePattern=log\\Application-%i.log
log4j.appender.dest1.rollingPolicy.ActiveFileName=log\\Application.log

Append log details in file rather in console in liferay 6.2 ga2

In liferay 6.2 to append log details in file instead of console i m inserting 2nd and 3rd line in log4j.properties file(please check line no below) and commenting the 4th line which is present in log4j.properties file by default . but fail to find log details in file. is this right way to configure ??.. if anyone knows solution for this please do reply .
my log4j.properties file look something like this.
log4j.rootLogger=INFO, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.FileAppender
log4j.appender.CONSOLE.File= E:\lrportal\workspace\liferay6.2\liferay-portal-6.2-ce-ga2\tomcat-7.0.42\logs\log-today.log
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c{1}:%L] %m%n
Define the root logger with appender file
log4j.rootLogger = INFO, FILE
Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
Set the name of the file
log4j.appender.FILE.File=${log}/log.out
Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true
Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug
Set the append to false, overwrite
log4j.appender.FILE.Append=false
Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
check below link for details
http://www.tutorialspoint.com/log4j/log4j_logging_files.htm

Wildfly 8.1 Log System

I am trying to make my Web Services Application Log using Wildfly 8.1 Final Log System Categories.
My App declares
private static final Logger LOGGER = Logger.getLogger("myapp");
It calls in parts of the code for
LOGGER.debug("something");
Using the log4j property file definition below it behaves as expected. The problem is when I access the Wildfly console (http://localhost:9990/console/App.html#logging) and update the Log Category named "myapp" from INFO to DEBUG. After tha I call my Web Service that calls the instruction LOGGER.debug("something"); inside it´s code.
The expected behavior is to show the "something" message written in my log file, but it does not happens because it behaves obeying only the log4j property file supplied below.
How to fix this?
#------------------------------------------------------------------------------
#log4j.rootLogger=DEBUG, stdout
log4j.logger.org.hibernate=INFO, stdout, myapp
log4j.logger.org.hibernate.SQL=INFO, stdout, myapp
log4j.logger.org.hibernate.type.descriptor.sql=ALL, stdout, myapp
log4j.logger.myappaudit=INFO, myappaudit
log4j.logger.myapp=INFO, stdout, myapp
log4j.logger.performance=INFO, myapp
#
# default Appender
#------------------------------------------------------------------------------
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.SSS} %c{1} [%p] %C{1}.%M - %m%n
#
# Appender for myappaudit
#------------------------------------------------------------------------------
log4j.appender.myappaudit = org.apache.log4j.DailyRollingFileAppender
log4j.appender.myappaudit.File = ${jboss.server.log.dir}/logs/myappaudit.log
log4j.appender.myappaudit.Append = true
log4j.appender.myappaudit.DatePattern = '.'yyy-MM-dd
log4j.appender.myappaudit.layout = org.apache.log4j.PatternLayout
log4j.appender.myappaudit.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} [%p] %m%n
#
# Appender for application
#------------------------------------------------------------------------------
log4j.appender.myapp = org.apache.log4j.DailyRollingFileAppender
log4j.appender.myapp.File = ${jboss.server.log.dir}/logs/console.log
log4j.appender.myapp.Append = true
log4j.appender.myapp.DatePattern = '.'yyy-MM-dd
log4j.appender.myapp.layout = org.apache.log4j.PatternLayout
log4j.appender.myapp.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss.SSS} %c{1} [%p] %m%n
#Spring logs
log4j.logger.org.springframework=DEBUG
log4j.logger.org.springframework.security=DEBUG
log4j.logger.org.springframework.ws=DEBUG
#Spring Transaction Logs
log4j.logger.org.springframework.orm.jpa=DEBUG
log4j.logger.org.springframework.transaction=DEBUG
#Spring Async Logs
log4j.logger.org.springframework.scheduling=ERROR
You will not be able to use the logger options in the console. The reason is that if you include a log4j config an entirely isolated logging instance is created for that deployment. If you want to use jboss's tools, you must remove your logging config and define in the standalone or domain xml. Alternatively, you can create your own servlet or mbean that is part of your deployment to make runtime adjustments to the logging level or any other changes, e.g adding or removing appenders
So, to be clear, are you packaging a log4j.properties file with your application? It that being respected correctly, but you hoped to use the jboss console to amend the logging level at runtime?

Eclipse4 RCP: Configure log4j with fragment plugin

So this is a follow up issue that arrised with this question: Using log4j in eclipse RCP doesn't work.
So far I'm able to use the log4j API inside my RCP but when running it with the following command
Category CAT = Category.getInstance(getClass().getSimpleName());
CAT.debug("Application has been started");
I get that exception:
No appenders could be found for category (MyPlugin).
Please initialize the log4j system properly.
I have created a fragment plugin containing a file called log4j.properties and the fragment host is the plugin containing the log4j API. The property file lives in the "root" of the fragment plugin
My log4j.properties file looks like that:
# Set root logger level to debug and its only appender to default.
log4j.rootLogger=debug, default
# default is set to be a ConsoleAppender.
log4j.appender.default=org.apache.log4j.ConsoleAppender
# default uses PatternLayout.
log4j.appender.default.layout=org.apache.log4j.PatternLayout
log4j.appender.default.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Any ideas what I'm doing wrong?
I am not sure, what is the purpose of using fragment for holding configuration. Try to place your log4j,properties (note that the name of the file is misspelled in your answer) directly to your plugin root folder. In the Activator.start() execute following code:
public void start(BundleContext context) throws Exception {
super.start(context);
URL installURL = plugin.getBundle().getEntry("/");
String installPath = Platform.asLocalURL(installURL).getFile();
PropertyConfigurator.configure(installPath +"/log4j.properties");
}
First of all, category documentation says: This class has been deprecated and replaced by the Logger subclass.
Secondly, Category has to be mapped to an appender:
log4j.category.MyPlugin=info,MyPlugin
log4j.appender.MyPlugin=org.apache.log4j.DailyRollingFileAppender
log4j.appender.MyPlugin.layout=org.apache.log4j.PatternLayout
log4j.appender.MyPlugin.layout.ConversionPattern={%t} %d - [%p] %c: %m %n
log4j.appender.MyPlugin.file=C:/logs/MyPlugin.log
log4j.appender.MyPlugin.DatePattern='.' yyyy-MM-dd-HH-mm

Creating an application specific log under Tomcat 7?

I'm trying to capture logging messages, stdout, and stderr for my webapp, which is running under Tomcat 7.
I have a log4j.properties file in my war file, at WEB-IN/classes/log4j.properties, pretty much copied from the log4j docs, except with a filename "myapp.log":
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.File=${catalina.home}/logs/myapp.log
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
WEB-INF/lib includes log4j-1.2.14.jar and commons-logging-1.0.4.
I'm expecting to see myapp.log appear in Tomcat's log directory, but it's not. Please explain to me what I'm doing wrong.
You're using a ConsoleAppender where you should be using a file appender:
log4j.appender.A1=org.apache.log4j.RollingFileAppender

Resources