where to put log4j.properties- for use in java desktop app - log4j

I am trying to use log4j logging framework in a java desktop application.
When I run the code I am getting a message
log4j: log4j:WARN No appenders could be found for logger
I googled for the above error and found that it occurs when the log4j.properties file is not found by the app.
I am creating the desktop app using Eclipse. Currently log4j.properties is within the same folder that contains the source (in 'src' folder). Where should I put this file to make it work properly?

log4j will use a Default Initialization Procedure to find your configuration file. The documentation notes:
The preferred way to specify the default initialization file is through the log4j.configuration system property
If this property is not set - log4j will look at the classpath. In your case you have to place the file in the src directory (assumed that the content will be put in the classes path while you build the system). If you use maven place the file to the src/main/resources directory.
As you placed your file to the src directory check, that the file get transferred to the top level directory of your compiled classes.

Keep the properties file in the classes folder i.e. the folder that contains the classes (specially the main class).
Another reason for getting this error may be the wrong appenders or an appender without setting.

Related

WS02 ESB - How to get Custom java class property file on the classpath

I have loaded a custom jar file into WSO2 by placing it into the /repository/components/lib directory, performing a restart. I then call that class from a script mediator using inline groovy. The groovy script recognizes the class, however the custom class is attempting to load a properties file that must be on the classpath. I have put that property file nearly everywhere but I keep getting an error that it cannot find the file on the classpath.
I am running the standalone WSO2 ESB 4.7.0. I have put the file as part of the jar, I have also attempted to place it in several directories within the WSO2 file structure as well. All to to avail.
you could try to register a resource in the carbon registry and add a Property to this Resource. Basically there are two ways (in java...):
Here is an example how to connect to the registry via a service with the PropertiesAdminServiceStub: http://www.massapi.com/class/org/wso2/carbon/registry/properties/stub/PropertiesAdminServiceStub.java.html
The most important here is that you authenticated your user, the result is a cookie which yoou have to add to the stub.
The other would be something like this (probably a duplicate of your question)
I am unable to get the list of services with in the applicaton i.e.; wso2 governance registry? I am working with binary code
The last one asumes that the carbon-context is available, means you are running the search inside the wso2 like a feature for example.
Unfortunately there is no place to put that properties file. Luckily this jar file, is an in house entity. It was written to search the classpath for the properties file and upon not finding one on the classpath to throw an exception. We ended up rewriting the code that loads the properties file to upon not finding the file on the classpath to search in a directory which we specified as a system environment variable in the wso2server.sh file. Not very elegant, but it is working perfectly.

Log4j Properties File Configuration

I'm using Log4j for logging my system events.
Initially I used the Log4j properties file and then shifted to log4j.xml file.
My application has another property file - system.properties - that takes the configuration parameters.
I want the log output file to be specified in the application properties file - system.properties and refer the properties file in the log4j.xml file and also update the configuration.
Is it possible with this log4j.xml configuration?
You can do it by using ant scripts. Load the system.properties file in ant and replace the value inside the log4j.xml file.
Refer to this link on how to update XML file.
How to update an XML document with Ant

log4j:WARN Please initialize the log4j system properly; still Log file created,but not in UNIX

I'm developing web application which has commons-logging.jar and for logging log4j.jar.
I got the following message when server start up.
log4j:WARN No appenders could be found for logger (org.apache.struts.util.PropertyMessageResources).
log4j:WARN Please initialize the log4j system properly.
But still log file is created and the format also same as specified in the log4j.properties.
The application log file is creating in Windows environment, But not in Unix environment.
Why it is not creating log file in UNIX ? Folder has write permissions..
Any idea?
Laxman Chowdary
The message you get on server startup is just a warning, your log file should be created inspite of it (you can find an explanation for the message in this post).
Why the file is created in Windows but it doesn't get created in Unix could be caused by lots of reasons: permissions for the user under which your application is running are first, maybe the configured path is still a Windows path (e.g. containing C:\ maybe), perhaps you meant to use an absolute path and forgot to prepend the / to it...
it's hard to say without seeing your configuration. Check these first and maybe update the question with the configs you are using.
Sometimes what can happen is that you use a relative path for the file and the file gets created relative to some folder in Windows and you expect it to be the same in Unix. But the "current folder" might be another in Unix. Maybe the file gets created but it's located in another place? Try searching for it on disk...

logging 2 web apps to 2 different files

I have 2 web applications built from the same source tree within tomcat, which each use a PropertyConfigurator loading their respective /webapp/WEB-INF/classes/log4j.properties.
Currently tomcat is configured to use one console appender for the whole container, and both app's log4js just write to the console:
log4j.rootCategory=info, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
This means I end up with one big catalina.out for the whole container.
I would like to configure each of the applications log4js so that they append to their own separate rolling files.
I'm aware I can use system properties substitution within log4j.properties however isn't system properties shared across the VM and therefore not threadsafe between the 2 applications within the container?
Can someone suggest a tidy solution which allows me to configure the 2 applications to log to separate files, preferably with the application's context name within the log's file name.
thanks, p.
Where is your log4j.jar and commons-logging.jar placed in tomcat directory? And which version of tomcat you are using?
If you have one copy of these jar inside common/lib than surely catalina.log would be used by all logging since tomcat's log4j.properties file is only the one getting configured for both server and webapps under it.
Assuming this scenario holds valid in your case, copy log4j.jar and common-logging.jar under WEB-INF/lib for both web applications.
For using application name within log's file name, since separate log4j.properties file is used for each application, name your logfiles as you like in log4j.properties file against FileAppender or RollingFileAppender.

Logging via FileAppender

here is an easy question coming:
i am trying to use log4net to log the infos on a file. i wrote
< file value="log-file.txt" /> into my appender tag in app.config. and now wondering where the log-file.txt is positioned and whether it is created automatically or i should create it by myself.
i am using c# - wpf
It should be in the Debug\Bin or Release\Bin folder.
if its not there, try specifying full path.
The file will automatically be created if it doesn't exist.
It may require that the application has write permission to the folder where the logfile is placed.
As Orentet mentions this is normally the bin folder.

Resources