How to use log4j in glassfish - java

I am trying to deploy a tomcat servlet in glassfish and after a couple of weeks making changes I did it.
Now I have another problem. I am using log4f to create log files to store my logs and it doesn't work.
I read that I should make some modifications but I don't know what to do exactly.
I have my log4j.properties and logback.xml files. I have other 2 servlet also with this library and also writing to their own files fine in tomcat. So I need an explanation valid for any servlet.
If you need any config file to be shown here, just tell me. I don't know what to show you.

GlassFish does not provide Log4J support by default can you check this:
https://blogs.oracle.com/naman/entry/configure_log4j_for_use_in

Glassfish admin console -> Configurations -> Server-config -> Logger Settings -> Log levels -> Add Logger -> org.hibernate.type : FINEST

The link #Gabriel gives was very helpful but the log4j.properties provided there did not work for me. This did:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 Configuration
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=SERVER: %d{DATE} %-5p %c{1} : %m%n

Related

Log4J Properties not found / no effect

I've got a Web application with Tomcat and would like to add Log4J2. Logging to the console by default works just fine. Although, I've added a log4j2.properties to add a File appender to my project and write the log to a dedicated location. Unfortunately, it seems that my properties-File has no effect.
Path to properties-File: /WEB-INF/log4j2.properties
Configs of log4j2.properties
name=Log4j2PropertiesConfig
appenders=file
appender.file.type=File
appender.file.name=FileLogger
appender.file.filename=logs/app.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=%d [%t] %-5p %c - %m%n
rootLogger.level=all
rootLogger.appenderRefs=file
rootLogger.appenderRef.file.ref=FileLogger
How I create the logger inside a class:
private val logger: Logger = LoggerFactory.getLogger(this.javaClass)
What am I doing wrong?
try to put the file log4j2.properties into src/main/resources. It should allocate in classpath
https://www.codejava.net/coding/how-to-initialize-log4j-for-java-web-application
The tutorial solves my problem as it defines a way on how to keep the properties in the WEB-INF.

Set JCS log level to ERROR - log4j

I have implemented JCS in my J2ee application which uses log4j for logging.
My Requirement
Set the application rootLogger in DEBUG level and jcs logs in ERROR mode.
What is tried
Tried the following in log4j properties
log4j.category.org.apache.common.jcs=ERROR
log4j.logger.org.apache.common.jcs=ERROR
But nothing is affecting the logging.
Whenever the cache access happens, it logs a bunch of returning first node messages.
Note : I am using the latest JCS commons-jcs-core-2.0-beta-1.jar
This is my complete log4j.properties
log4j.rootLogger=DEBUG, A1
# Use Console Appender for development
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern= %d [%t] %-5p - %m%n
log4j.logger.net.sf.jasperreports=ERROR
log4j.category.org.apache.common.jcs=ERROR
I missed an "s" in commons. Correcting it solved my problem.
Solution
Both the below statements can be used to configure JCS log level in log4j.
log4j.category.org.apache.commons.jcs=ERROR
log4j.logger.org.apache.commons.jcs=ERROR
JCS has a dependency on commons-logging for logging, and not log4j. Hence, log4j configurations won't matter unless you redirect commons-logging to log4j. To do that, create a file named commons-logging.properties and add the following in that.
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4jLogger

restrict logs using log4j

I'm using log4j for logging, and it is the first time I'm working on it.
I want to log particular lines from the code, say for exmple
log.debug("this is my an example");
Only want these lines from my project in my log file. Is it possible using log4j?
Here is my log4j.properties
log4j.rootLogger=DEBUG, CA
log4j.appender.CA =org.apache.log4j.RollingFileAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.File=myLocation/logs.log
log4j.appender.CA.ImmediateFlush=true
log4j.appender.CA.Append=false
log4j.appender.CA.layout.ConversionPattern= %d{ABSOLUTE} %5p %c - %m%n
aslo can we route the logging to separate file for each run?
Set loglevel in your code as below and make it as a static block or something where it should load at the startup
private static org.apache.log4j.Logger log = Logger.getLogger(LogClass.class);
log.setLevel(Level.Debug);
and for specific package restrictions of log, you can do like below
Suppose you have a package a.b.c
To specify logging level for this package as debug, add the below line in your log4j.properties
log4j.logger.a.b.c=debug

Log4j doesn't log INFO Level

I have the following log4j.properties file, for an application deployed in WebSphere Portal:
log4j.rootLogger=DEBUG, InfoAppender, DebugAppender
log4j.appender.InfoAppender=org.apache.log4j.RollingFileAppender
log4j.appender.InfoAppender.Threshold=INFO
log4j.appender.InfoAppender.File=C:/info.log
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoAppender.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
log4j.appender.DebugAppender.Threshold=DEBUG
log4j.appender.DebugAppender.File=C:/debug.log
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d %p [%c] - %m%n
When I code, I define the logger at class level:
private static Logger logger = Logger.getLogger(IWannaLogThis.class);
And I log INFO messages with this:
logger.info(theObjectToLog);
When I deploy my application, the debug.log file gets everything I log with logger.debug() but ignores everything I write with logger.info(). On the other side, the info.log file keeps empty.
The weirdest thing is that in debug.log and info.log appears some INFO and DEBUG messages made by some JARS (like Hibernate Validator) I had in the classpath, but just ignores everything I try to log in my code.
Any ideas?
This is most likely a classloading-related problem. WebSphere Portal uses Log4J internally, so I'm guessing that you end up using WebSphere Portal's provided Log4J JAR file as well as its own Log4J properties.
You can verify that by adding the following to the JVM arguments of the server instance:
-Dlog4j.debug=true
And then inspect the SystemOut.log file. Log4J will spit out lots of tracing information about the configuration file(s) it reads.
The best way to avoid this is to do the following:
Bundle the Log4J JAR file with your application.
Associate a Shared Library with the server. In that Shared Library, place your Log4J configuration file.
As an alternative to step 2, you can bundle your Log4J configuration file with the application itself, however that would carry its own drawbacks (for example, having to repackage your application whenever you perform a Log4J configuration change).
Another common problem is that the JARs you have in your classpath also use log4j and also have their own appenders set. So depending on the settings that they use, and the packages that your classes reside in, this may lead to the problem you describe.
So:
Make sure that your package names are unique and not used by any of the third party libraries.
Check the log4j settings in all libraries in your classpath. They should not contain general settings which override yours.
Make sure your loggers use your log4j.properties (you can be sure if changes you make in your file affect your loggers as expected).
If you can, make sure that your log4j stuff loads last, in case any of the third party libs reset the configuration. They shouldn't, but who can stop them.
Normally, it should be one of these things. Post more explicit example if it doesn't work.
Good luck!
What I have done in the past is set specific logs for the classes I want to log. It sounds like you can try setting your root logger to INFO and see if that gets you the messages you want. Here's a little bit of my log4j property file. I set a logger for each class and assign it to my "data" appender, which defines the log layout. In the loggers I specify specific classes I want to log and set their Log level individually. Any class that logs that is not defined in the Loggers I have use the default log level for the rootCategory.
log4j.rootCategory=INFO, rollingFile, stdout
#GetData Loggers
log4j.logger.com.myapp.data=INFO, data
log4j.logger.com.myapp.data.SybaseConnection=DEBUG, data
log4j.logger.com.myapp.data.GetData=ERROR, data
# data appender
log4j.appender.data=org.apache.log4j.RollingFileAppender
log4j.appender.data.layout=org.apache.log4j.PatternLayout
log4j.appender.data.File=c\:\\Program Files\\MyApp\\logs\\MyApp-data.log
log4j.appender.data.Append=true
log4j.appender.data.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
you root logger opens the log properties in the debug mode,
use INFO instead of DEbug in the first line of your properties file.

Tomcat App says "Unable to configure the logging system. No log.properties was found."

I have an embedded tomcat app and whenever I start it up I see this error printed to the console twice:
Unable to configure the logging system. No log.properties was found.
It seems stupid, but I've done some googling and searched stackoverflow and can't seem to find someone experiencing this problem.
My main class Looks roughly like this:
public class AuthServerEntryPoint {
static {
org.apache.log4j.PropertyConfigurator.configure("conf/log4j.properties");
}
public static void main(String[] args) {
// ...
}
"conf/log4j.properties" contains a seemingly valid configuration:
log4j.appender.mainAppend=org.apache.log4j.ConsoleAppender
log4j.appender.mainAppend.layout=org.apache.log4j.PatternLayout
log4j.appender.mainAppend.layout.ConversionPattern=%d %p [%t] %c -- %m%n
log4j.appender.fileAppend=org.apache.log4j.FileAppender
log4j.appender.fileAppend.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppend.layout.ConversionPattern=%d %p [%t] %c - %m%n
log4j.appender.fileAppend.file=logs/myservice.log
log4j.rootLogger = info, fileAppend
log4j.logger.com.mycompany.myservice = debug, fileAppend
And the logging actually does work - i.e., logs are correctly written to myservice.log. So what gives?
Thanks!
-Carl
By embedded Tomcat app, do you mean that you are starting Tomcat from Java code and are using the class org.apache.catalina.startup.Embedded?
If yes, my guess is that Tomcat might not be able to find its logging configuration file that is set up in catalina.sh (or equivalent) when using the scripts:
LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
The odd part is that this file is called logging.properties, not log.properties, so I'm really not sure. I didn't check Tomcat sources though, maybe they are doing some kind of black magic in there.
Could you just try to rename $CATALINA_BASE/conf/logging.properties into log.properties (or not) and to put it on the classpath of your app (or to set -Djava.util.logging.config.file)?
By default, log4j will look for a logging properties file on the classpath. Put your webapp's logging properties config file into its WEB-INF/classes directory; e.g.
$CATALINA_HOME/webapps/<yourApp>/WEB-INF/classes/log4j.properties
This is the simple approach to getting a webapp to use Log4j is recommended by the referenced documentation. And it is the approach I use myself for webapp logging.
There are various other ways to configure Log4j logging on Tomcat as well. It is possible that your Tomcat has been (partly) configured to use one of them, but something has not been done quite right.
Configuring Tomcat' log4j logging via the system properties is an option that avoids figuring out where log4j is looking ... and what is going wrong. But you are then stuck with creating/using a custom launch script or having to remember to set environment variables before launching Tomcat.
References:
Configuring logging on Tomcat 5.5
Configuring logging on Tomcat 6
The "Default Initialization under Tomcat" section of the Log4j Manual

Categories