I'm trying to use SLF4J with log4j in a Bean inside a EJB, I have already tried to place the log4j.properties file in quite a few places but I keep getting this error in the Glassfish Server console:
Grave: log4j:WARN No appenders could be found for logger (uy.ort.enviosya.cadets.services.CadetsBean).
Grave: log4j:WARN Please initialize the log4j system properly.
Grave: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info
This is my properties file:
# LOG4J configuration
log4j.rootLogger=DEBUG, Appender1,Appender2
log4j.appender.Appender1=org.apache.log4j.ConsoleAppender
log4j.appender.Appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender1.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
log4j.appender.Appender2=org.apache.log4j.FileAppender
log4j.appender.Appender2.File=C:/Users/Log4jWebDemo.log
log4j.appender.Appender2.layout=org.apache.log4j.PatternLayout
log4j.appender.Appender2.layout.ConversionPattern=%-7p %d [%t] %c %x - %m%n
The questions related to this problem say I should place it in the classpath but honestly I don't know what they mean by that (totally new to EJB).
This is what I'm doing in the bean:
#Stateless
public class SomeBean implements SomeBeanRemote {
private static final Logger LOGGER = LoggerFactory.getLogger(SomeBean.class);
#Override
public someMethod() {
LOGGER.info("Prueba");
...
}
...
}
Plus, should I be placing the file inside the EJB? What if I want to modify the destination of the Appender2 then?
I'm using Netbeans 8.2.
Update 1
I have placed it inside the META-INF folder of my ejb, and from what I can see it is present in that same folder in the Cadets-ejb.jar that I'm deploying inside .ear.
But I'm still getting the same error.
Partial folder structure:
Some
build
Some-ejb
build
dist
nbproject
src
conf
META-INF
java
test
dist
lib
nbproject
src
Update 2
I managed to get it working but by placing the properties file by hand in the correct place in the build folder, which means I need to do this every time I do a clean-build.
I tried placing it in the src folder but when I build it doesn't get copied, only what is inside src/confgets copied and that is the wrong location for this file.
Why is the file in src being ignored on build?
This is my structure now:
Some
/build
/Some-ejb
/build
/dist
/nbproject
/src
/conf
/META-INF
MANIFEST.MF
/java
/log4j.properties
/test
/dist
/lib
/log4j.jar
/nbproject
/src
I now that the file, once the jar is created needs no be at the same level that META-INFand my class packages.
Solution
I solved it by placing it here
Some
/build
/Some-ejb
/build
/dist
/nbproject
/src
/conf
/META-INF
MANIFEST.MF
/java
log4j.properties
/test
/dist
/lib
/log4j.jar
/nbproject
/src
After I googled a lot, i think i found a solution for this:
FOR A GLOBAL CONFIGURATION
If your log4j library is included within your EAR file, then check your app server's JVM properties to ensure the log4j.configuration property is set:
Login to the Glassfish Admin Console (http://[hostname]:4848/)
Click on 'Server(Admin Server)'-> Click on 'server-config' Configuration -> JVM Settings -> JVM Options. (depends of your server version, but the important is JVM Settings > JVM Options)
If an entry for -Dlog4j.configuration exists, verify that it contains the location of your log4j.properties file
If an entry do not exist for -Dlog4j.configuration, create one. It must follow the following template: -Dlog4j.configuration=file:///path/to/your/log4j.properties
Restart the GlassFish.
Deploy sample app and check if the Log4J statements are now available.
In case your project doesn't contain log4j library
Copy log4j.jar inside the GLASSFISH_HOME/lib.
FOR A PROJECT
For instance, you can have the following structure:
Some
/build
/Some-ejb
/build
/dist
/nbproject
/src
/conf
/META-INF
MANIFEST.MF
/java
/test
/dist
/lib
/log4j.jar
/nbproject
/src
/log4j.properties
In order to include the log4j-files into your classpath, you have to put the following into the META-INF/MANIFEST.MF on your EJB Project:
Class-Path: src lib/log4j.jar
Yes: it’s relative to the EAR, not to the EJB Project.
Do not put the log4j.properties itself in the classpath, only the directory that contains that file.
Related
I have a package called Resources in which I have placed my log4j.properties file. When I run my code, I get the following error:
log4j:WARN No appenders could be found for logger.
log4j:WARN Please initialize the log4j system properly.
How should I update the classpath to include Resource folder ?
Solution tried:
I added the log4j.properties file directly under 'source' and it worked.
Here is the contents of properties file:
#Application Logs
log4j.logger.devpinoyLogger=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.RollingFileAppender
log4j.appender.dest1.maxFileSize=5000KB
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=C:\\Users\\kagarwal\\Desktop\\Application.log
log4j.appender.dest1.Append=false
Right click on the folder, select Build Path then Use as a Source Folder.
Just remember that when you will deploy you will need to create a directory for resources and add it to class path.
You should also consider to use maven for your java project, it may seem overkill initially but it will pay off in the long run.
I'm trying to load logback.xml file from outside of my executable myapp.jar file.
The myapp.jar has a META-INF/MANIFEST.MF file roughly like this:
Manifest-Version: 1.0
Class-Path: logger-config lib/lib/jcl-over-slf4j-1.7.18.jar lib/slf4j-api-1.7.18.jar
Main-Class: com.mycompany.MyAppMain
and resides in
my-app/
lib/
jcl-over-slf4j-1.7.18.jar
slf4j-api-1.7.18.jar
logger-config/
logback.xml
myapp.jar
(there is a lot of other jar dependencies, cut those out).
Now, if I run the application using java -jar myapp.jar, it starts OK, but it does not pick up the logback.xml file.
From their docs:
If no such file is found, it checks for the file logback.xml in the classpath..
( http://logback.qos.ch/manual/configuration.html )
I know I can override this using logback.configurationFile, but is there a way to just put the XML file to classpath so that logback loads it automatically? It seems to work only if I package the logback.xml to myapp.jar (placing it in src/main/resources dir), but then the config file will be propagated to other JARs that uses myapp.jar as a dependency.
Maybe you just cut it out, but your classpath does not specify the logback-classic dependency.
You will have to make sure logback appears before slf4j in your class-path definition. Also, note the trailing slash for the logger-config path - logback will not find your logback.xml without it (see here for details).
Something like this should work:
Class-Path: lib/logback-classic-1.1.6.jar lib/logback-core-1.1.6.jar lib/jcl-over-slf4j-1.7.18.jar lib/slf4j-api-1.7.18.jar logger-config/
I'm working on a Eclipse RCP application, and I would like to have a customized ConsoleAppender, so I can redirect all logs to the log window.
The log4j plugin and the log4j fragment (containing the log4j.properties) creates a unit which I manage to use successfully.
I've also created an 'extension' plugin containing my code to capture the log-data. Behold the 2 plugins and the fragment below.
rcp_external_log4j (plugin containing the jar file)
log4j-1.2.16.jar
rcp_external_log4j_fragment (containing the log4j.properties file)
log4j.properties (which points to VirtualConsol)
rcp_external_log4j_extension (plugin containing the VirtualConsol)
src/VirtualControl.java (which extends ConsoleAppender)
When I execute my project from within the Eclipse debug environment everything works fine. The VirtualConsole forwards all logging data as expected.
However, when I try to export the project with the 'Eclipse product export wizard' to a standalone executable I get the following problem:
Problem Occured
'Export Product' has encountered a problem.
A cycle was detected when generating the classpath
rcp_external_log4j_extension
rcp_external_log4j
rcp_external_log4j_extension
The VirtualConsole extends ConsoleAppender and is also called from other parts of the code.
The VirtualConsole is located in rcp_external_log4j_extension and uses log4j due to the extending of ConsoleAppender.
The rcp_external_log4j uses the rcp_external_log4j_extension, due to that the rcp_external_log4j plugin is bundled with the rcp_external_log4j_fragment, which refers to the VirtualConsole in the log4j.properties file.
Question: How can I write my own ConsoleAppender without getting a circular dependency? Can I put the code in the fragment? Can I put the code in the plugin containing the jar file (rcp_external_log4j)? I've tried those 2 attempts but without success...
Grateful for any help
Problem solved. src is put in the same plugin as the jar file.
rcp_external_log4j (plugin containing the jar file)
log4j-1.2.16.jar
src/VirtualControl.java (which extends ConsoleAppender)
rcp_external_log4j_fragment (containing the log4j.properties file)
log4j.properties (which points to VirtualConsol)
and 'src' added to the classpath of rcp_external_log4j. Seen in the MANIFEST.MF as
Bundle-ClassPath: log4j-1.2.16.jar,
src/
This way the VirtualConsole may be used both from the fragment and from the other code.
And the build.properties should contain:
jars.compile.order = src/
source.src/ = src/
output.src/ = bin/
for the code to be included in the exported product.
I don't think rcp_external_log4j_fragment needs to have rcp_external_log4j_extension in its build path, only in the manifest. This should remove the cycle.
According to log4j manual, I should put log4j.properties to the src folder. I copied this file to all the possible places I think it will affect log4j. However, this does not work.
TestEM class contains many unit test functions (I use testng). I run one of these test functions which references a class in the feedback.strategy package.
Here is the content of the log4j.properties file:
#log for class1
log4j.category.Demo1=DEBUG, dest1
log4j.appender.dest1=org.apache.log4j.FileAppender
log4j.appender.dest1.File=C:/Users/Asus/workspace/FeedbackProcess/logs/class1.log
log4j.appender.dest1.layout = org.apache.log4j.PatternLayout
log4j.appender.dest1.layout.ConversionPattern= %d %p [%t] (%c) \u2013 %m%n
This is mostly a question of convention and/or personal preference. What I am accustomed to is to create another source directory (e.g. config or resources) in the project root (next to src and test), then place the log4j.properties there. This can by done by you right-clicking on the newly created folder and choosing Build Path -> Use as Source Folder. Optionally you can specify some inclusion/exclusion patterns too.
Log4j looks for the properties file on the root of the classpath by default. With the above setup the properties file gets copied to the output directory and it will be on the root of your classpath during development.
Later during the deployment the log4j.properties would get bundled in the jar/war file too. You might want to override the bundled properties by specifying an alternative config folder on the classpath during runtime. (see this other question and the docs about the order entries take precedence on the classpath)
You need to put the log4j.properties file on the application classpath.
maybe the file log4j.properties is being read correctly and the problem is with the configuration in log4j.properties file. Does adding this line to the begining of log4j.properties make any difference (assuming that you are have some logging statements in your class files)
log4j.rootLogger=DEBUG, dest1
Place log4j.properties in WEB-INF\classes of the project .
Put log4j-xx.jar under WEB-INF\lib see here for details
I have a Java Project that was added to the Java Build Path of a Web Project.
On the first Java project I added the Log4J JAR file to the Java Build Path and since this project was added to the Java Build Project of the Web Project, the JAR file was automatically added to the Web Project Java Build Path also.
On the Web Project I have a Web Service that instantiates a class of the Java Project. That class has a simple Logger, and it works correctly.
Now, I am trying to create a properties file named log4j.properties to configure the Logger, Appender and LayoutPattern.
Whenever I try to call a method of the instantiaded class I get this error on the console:
log4j:ERROR Could not read configuration file [log4j.properties].
What am I doing wrong?
Here's the log4j properties file:
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Sorry but it was a false alarm...
Like I said, the project where the class that instantiates the logger resides is added as a dependency o a main project where the web services are defined.
As a result that project is published on a JAR file and with the suposed solution I mentioned:
PropertyConfigurator.configure(getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "log4j.properties");
I get a path like:
C:/project_path.jar/log4j.properties.
Obviously the propertied files still isn't found...
Sory... Still working on a solution
If using log4j 2.x, you need to have a file called log4j2.xml.
log4j.properties won't do it.
Place your log4j.properties file in your classes directory if using unpacked WAR, else place it in the src folder (root folder for your java classes).
The only way I found to solve this was to put the log4j.properties file in the src root folder.
Then on the class that instantiates the logger the following line:
PropertyConfigurator.configure("log4j.properties")
... had to be changed to:
PropertyConfigurator.configure(getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "log4j.properties");
And finally the file was found.
Thanks for the insight Pål
Ok, sometimes the obvious answer is the one you least expect.
As it turned out I simply needed to remove the PropertyConfigurator.configure(xxx) line and place the log4j.properties file on the src folder of the dependency project.
Thanks