Using log4j for different applications - log4j

I have a problem with log4j loggin and I hope you can help me on this.
This is the scenario: I have 3 different applications (in other words 3 .jar) A, B and C. A is always running and from time to time calls B and C to execute them. Each of them have its own log4j.properties (with the path duly included in the MANIFEST) but Log4j only takes the properties of the main app A into consideration, ignoring the rest. I need to know if I can force Log4j to use for each app its own properties. I have seen something related using DOMConfigurator, but if I'm not wrong this only works with xml files and I'm using properties.
Thanks in advance

There are several Configurators, one of which is the PropertyConfigurator.

I need to know if I can force Log4j to use for each app its own properties.
Yes, you can. When you launch the applications B and C, define this system variable at their command line:
-Dlog4j.configuration=B.properties
where B.properties is the file name of log4j properties file in B's classpath. Likewise for application C.
Edit: found this: Change location of log4j.properties

Related

Change .eclipse folder in Linux

How can I change the .eclipse folder in Linux? I tried adding this line:
-Dosgi.configuration.area=/directory/directory1/eclipse/.eclipse
at the top of eclipse.ini but it doesn't work. I've also tried adding it to various other places in the eclipse.ini but still no luck.
Edit
I have added this line:
-Dosgi.configuration.area=file:/directory/directory1/eclipse/.eclipse
immediately below -vmargs. When Eclipse starts, it now reads from the correct .eclipse location and if .eclipse does not exist there, it creates it. Unfortunately, after Eclipse has loaded, another .eclipse folder is created in my home folder and Eclipse then continues to read from that folder. I suspect that my eclipse.ini file is now correct but there is another file I need to change.
The simplest thing to do is probably pass java a different user.home so that all the other myriad of places that derive a location base it off of user.home. So instead of what you have, use this in .ini file:
-Duser.home=/directory/other/here
In addition to .eclipse, you will probably find other directories created in your overridden user.home, such as .p2, .oracle_jre_usage, etc.
Other notes:
-Dosgi.configuration.area is the changes the configuration area for Eclipse, it does not effect user area. You also probably don't want to change that setting away from the default unless you really want multiple configurations (read more below).
Additionally, the normal thing to do would be to use -configuration as an argument to eclipse{.exe} and let eclipse convert it to the appropriate VM argument.
You probably want -user though to override the user area. Have a look at locations in the Eclipse help for more info (quoted below).
However, there are still things that have individual control over their location, such as secure storage, which is controlled by the -eclipse.keyring command line argument.
Locations
The Eclipse runtime defines a number of locations which give
plug-in developers context for reading/storing data and Eclipse users
a control over the scope of data sharing and visibility. Eclipse
defines the following notions of location:
User (-user) {osgi.user.area} [#none, #noDefault, #user.home,
#user.dir, filepath, url]
User locations are specific to, go figure,
users. Typically the user location is based on the value of the Java
user.home system property but this can be overridden. Information such
as user scoped preferences and login information may be found in the
user location.
Install (-install) {osgi.install.area} [#user.home,
#user.dir, filepath, url]
An install location is where Eclipse itself
is installed. In practice this location is the directory (typically
"eclipse") which is the parent of the eclipse.exe being run or the
plugins directory containing the org.eclipse.equinox.launcher bundle.
This location should be considered read-only to normal users as an
install may be shared by many users. It is possible to set the install
location and decouple eclipse.exe from the rest of Eclipse.
Configuration (-configuration) {osgi.configuration.area} [#none,
#noDefault, #user.home, #user.dir, filepath, url]
Configuration
locations contain files which identify and manage the (sub)set of an
install to run. As such, there may be many configurations per install.
Installs may come with a default configuration area but typical
startup scenarios involve the runtime attempting to find a more
writable configuration location.
Instance (-data) {osgi.instance.area}
[#none, #noDefault, #user.home, #user.dir, filepath, url]
Instance
locations contain user-defined data artifacts. For example, the
Resources plug-in uses the instance area as the workspace location and
thus the default home for projects. Other plugins are free to write
whatever files they like in this location.
While users can set any of
these locations, Eclipse will compute reasonable defaults if values
are not given. The most common usecase for setting location is the
instance area or, in the IDE context, the workspace. To run the
default Eclipse configuration on a specific data set you can specify:
eclipse -data c:\mydata
You must put property definitions like this at the end of the eclipse.ini after the -vmargs line. If there is no -vmargs line you must add one.
So:
.... other lines ....
-vmargs
... other arguments
-Dosgi.configuration.area=/directory/directory1/eclipse.eclipse

How can I change the name and location of log4j.properties?

How can I change the name of log4j.properties and the location of this as well?
You can change its location like so:
java -Dlog4j.configuration=file:/path_to_file_here/log4j.properties YourApplication
You should also read the manual.
Regarding changing the name, this is how you can achieve this:
First, you must add the following line to your java runtime command:
-Dlog4j.configuration=test.properties
For example lets assume you are using log4j in your web application deployed on Tomcat.
Add the above mentioned line in the java runtime command to start up Tomcat:
C:\Tools\java\j2sdk1.4.2_01\bin\java.exe -jar
-Duser.dir="C:\Tools\Tomcat 4.1"
-Dlog4j.configuration=test.properties
-Djava.endorsed.dirs="C:\Tools\Tomcat 4.1\common\endorsed"
"C:\Tools\Tomcat 4.1\bin\bootstrap.jar" start
You will also possibly want to read this.
I know this is a really old post, but the first thread when I searched for the question. And my found solution is:
System.setProperty("log4j.configurationFile", "theNameIWant.properties");

Which config file to use for each GG example

Which spring-????-config.xml I should use to star GG nodes so the .net example GridClientApiExample works?
Each GridGain example provides a short description of how to run remote nodes in the example documentation.
Usually there are two ways to run remote nodes for the example. The first and, probably, the most convenient one is to run corresponding *NodeStartup class from IDE in the examples project. The name of startup class is specified in example documentation. The second way is to start a node with ggstart.{sh|bat} script with a configuration file specified in the documentation (if available).
GridClientApiExample works only with node started from IDE with ClientExampleNodeStartup, and there is a reason for it. The example expects a specific task class (org.gridgain.examples.misc.client.api.ClientExampleTask) to be in the node's classpath. Since this is an example-only class, it is not present in node classpath when running ggstart.{sh|bat}.
If for some reason you want to run a node with command line script for this example, you should build examples jar file and drop it to $GRIDGAIN_HOME/libs/ext (startup script will automatically pick up all additional libraries placed in this folder). Then you can use the same config which ClientExampleNodeStartup uses, namely examples/config/example-compute.xml
You can use ClientExampleNodeStartup or start node with ggstart.sh examples/config/example-compute.xml

how to switch log4net multiple log files using C# code?

I'm using log4net, I would like to have 2 logs,
- BasketballCustomer.log, for all Customers that plays Basketball;
- ChessCustomer.log, for all Customers that plays Chess.
, while for each customer, whether he/she plays Basketball or Chess, is only known until runtime.
I would like to have each log configured separately, about log file name, size, number, log level, etc.
Also, I'd prefer such set up done by C# code, not config file.
How could I do that?
I tried search on net, there are some articles but none meet exactly my requirements
- Log4Net and multiple log files talked about multiple log files but it does not toggle during runtime;
- Configure Log4net to write to multiple files is similiar but it's done in config file....
Please kindly suggest, many thanks!
You can do this by using an environment variable in the log4net.config and then set the value of the environment variable through the C# code
So somewhere in your C# class, do something like:
Environment.SetEnvironmentVariable("log_file_name", "MyLogFileName");
And then in the log4net.config that is used, specify the value to the name of the environment variable. The syntax would be something like this:
<param name="File" value="${log_file_name}".log/>

Logging with log4j on tomcat jruby-rack for a Rails 3 application

I just spent the better part of 3 hours trying to get my Rails application logging with Log4j. I've finally got it working, but I'm not sure if what I did is correct. I tried various methods to no avail until my very last attempt. So I'm really looking for some validation here, perhaps some pointers and tips as well -- anything would be appreciated to be honest. I've summarized all my feeble methods into three attempts below. I'm hoping for some enlightenment on where I went wrong with each attempt -- even if it means I get ripped up.
Thanks for the help in advance!
System Specs
Rails 3.0
Windows Server 2008
Log4j 1.2
Tomact 6.0.29
Java 6
Attempt 1 - Configured Tomcat to Use Log4J
I basically followed the guide on the Apache Tomcat website here. The steps are:
Create a log4j.properties file in $CATALINA_HOME/lib
Download and copy the log4j-x.y.z.jar into $CATALINA_HOME/lib
Replace $CATALINA_HOME/bin/tomcat-juli.jar with the tomcat-juli.jar from the Apache Tomcat Extras folder
Copy tomcat-juli-adapters.jar from the Apache Tomcat Extras folder into $CATALINA_HOME/lib
Delete $CATALINA_BASE/conf/logging.properties
Start Tomcat (as a service)
Expected Results According to the Guide
I should have seen a tomcat.log file in my $CATALINA_BASE/logs folder.
Actual Results
No tomcat.log
Saw three of the standard logs instead
jakarta_service_20101231.log
stderr_20101231.log
stdout_20101231.log
Question
Shouldn't I have at least seen a tomcat.log file?
Attempt 2 - Use default Tomcat logging (commons-logging)
Reverted all the changes from the previous setup
Modified $CATALINA_BASE/conf/logging.properties by doing the following:
Adding a setting for my application in the handlers line: 5rails3.org.apache.juli.FileHandler
Adding Handler specific properties
5rails3.org.apache.juli.FileHandler.level = FINE
5rails3.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
5rails3.org.apache.juli.FileHandler.prefix = rails3.
Adding Facility specific properties
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/rails3].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/rails3].handlers = 4host-manager.org.apache.juli.FileHandler
Modified my web.xml by adding the following context parameter as per the Logging section of the jruby-rack README (I also modified my warbler.rb accordingly, but I opted to change the web.xml directly to test things faster).
<context-param>
<param-name>jruby.rack.logging</param-name>
<param-value>commons_logging</param-value>
</context-param>
Restarted Tomcat
Results
A log file was created (rails3.log), however there was no log information in the file.
Attempt 2A - Use Log4j with existing set up
I decided to give Log4j another whirl with this new web.xml setting.
Copied the log4j.jar into my WEB-INF/lib folder
Created a log4j.properties file and put it into WEB-INF/classes
log4j.rootLogger=INFO, R
log4j.logger.javax.servlet=DEBUG
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=${catalina.base}/logs/rails3.log
log4j.appender.R.MaxFileSize=5036KB
log4j.appender.R.MaxBackupIndex=4
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss} [%t] %-5p %c %x - %m%n
Restarted Tomcat
Results
Same as Attempt 2
NOTE: I used log4j.logger.javax.servlet=DEBUG because I read in the jruby-rack README that all logging output is automatically redirected to the javax.servlet.ServletContext#log method. So I thought this would capture it, but I was obviously wrong.
Question
Why didn't this work?
Isn't Log4J using the commons_logging API?
Attempt 3 - Tried out slf4j (WORKED)
A bit uncertain as to why Attempt 2A didn't work, I thought to myself, maybe I can't use commons_logging for the jruby.rack.logging parameter because it's probably not using commons_logging API... (but I was still not sure). I saw slf4j as an option. I have never heard of it and out of curiosity, I decided to look it up. After reading about it briefly, I thought it was as good of a shot as any and decided to try it out following the instructions here.
Continuing from the setup of Attempt 2A:
Copied slf4j-api-1.6.1.jar and slf4j-simple-1.6.1.jar into my WEB-INF/lib folder
I also copied slf4j-log4j12-1.6.1.jar into my WEB-INF/lib folder
Restarted Tomcat
And VIOLA! I now have logging information going into my rails3.log file.
So the big question is:
WTF?
Even though logging seems to be working now, I'm really not sure if what I did is right. So like I said earlier, I'm really looking for some validation more or less. I'd also appreciate any pointers/tips/advice if you have any. Thanks!
We also did various experiments and finally settled with slf4j option. Coming from Java background we knew slf4j, so we didn't go further.
<context-param>
<param-name>jruby.rack.logging</param-name>
<param-value>slf4j</param-value>
</context-param>
btw, there is no need to copy slf4j-simple-1.6.1.jar into tomcat/lib or WEB-INF/lib when using slf4j-log4jxxx.jar
It's possible that your early tests with commons-logging were showing zero bytes files because of bufferSize.
If your entries get flushed to the file when you stop Tomcat then this is indeed the case and you are falling foul of the default 8Kb buffer.
To disable, for immediate flushing try...
org.apache.juli.FileHandler.bufferSize = -1
on the specific handler...
So for localhost...
2localhost.org.apache.juli.FileHandler.bufferSize = -1

Resources