Solved

Need logging object available to classes in my package

Posted on 2004-10-13
8
186 Views
Last Modified: 2013-11-23
Ok...this may be a stupid question, but I am getting frustrated. I am just learning Java and have been writing an application.

I am attempting to use log4j in my application and need some help. I want to create the Logger once then reuse it any number of different places in my application. The classes in my app are in different files and I just can't seem to figure out how to reuse the Logger object.

Does this make any sense? If not, let me know...I will try to explain further =)

I am hoping someone can explain or point me in the right direction.
0
Comment
Question by:emartin24
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:emartin24
ID: 12303098
Let me provide an example:

I have the following two files:

Main.java
------------------------------------------------------------------------
package com.myurl;
<import statements here>
public class Main {
  static Logger log = null;
  <other variable declarations here>

  public static void main(String args[]) {
    try {
      bis = new BufferedInputStream(new FileInputStream(propFile));
      props = new Properties();
      props.load(bis);
      PropertyConfigurator.configure(props);
      log = Logger.getLogger(Main.class);
    } catch (FileNotFoundException fnfe) {
    } catch (IOException ioex) {
    }
  }
}


MyClass.java
------------------------------------------------------------------------
package com.myurl;
<import statements here>
public class MyClass {
  static Logger log = null;
  <other variable declarations here>

  public void sampleMethod() {
      <how do I make is so I can use the Logger Object log here>
    }
  }
}

0
 
LVL 92

Expert Comment

by:objects
ID: 12303302
You can access the one in main as:

Main.log
0
 

Author Comment

by:emartin24
ID: 12304168
Do you recommend doing it that way. Are there other ways that would be better?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 75 total points
ID: 12304247
or you could

public class MyClass {
  static Logger log = Logger.getLogger(Main.class);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Assisted Solution

by:RuadRauFlessa
RuadRauFlessa earned 75 total points
ID: 12305804
Log4J has other means of obtaining the same logger.


When you initially create the logger you have to give it a name
      log = Logger.getLogger(Main.class);

Then in the other classe you should use the same name when creating new instances of the logger. That would actually retrieve a logger if it finds one with the given name otherwise Log4J would create a new one. So all you need do is pass it the same name as you did in the beginning and you should be set for logging. Which means the value of Main.class.
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 100 total points
ID: 12305836
You should create a seperate Logger for each class, so that the log output can be very simply matched back to where the log was generated....

using the example by Objects,

package com.test.testing;
public class MyClass {
    // definitely use static initialisation!
    static Logger log = Logger.getLogger( MyClass.class );

    public void test() {
        log.info( "Test called" );

This will create a logger identified as "com.test.testing.MyClass", and depending on your log4j configuration, you can identify the log output with this logger's identification (also, you can use the logger indentification when controlling log level - e.g. enable "INFO" level logging on all "com.test", but for "com.test.testing" enable DEBUG level...

btw, you can specify the log4j configuration as a system property (which would save you having to do the "PropertyConfigurator" work. Otherwise, if you are going to programatically initialise, I'd use "configureAndWatch()" rather than just configure() - as the former periodically checks the configuration file for changes, thus allowing you to dynamically change the log output of the application without having to restart the application...

Cheers,
C.
0
 

Author Comment

by:emartin24
ID: 12311388
cjjclifford,

Can you eplain what you mean by " you can specify the log4j configuration as a system property". This is going to be an application that can be used on a number of servers...does this still apply?
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12317004
Hi,

What I mean by that is that the configuration file can be specified using a system property to the JVM, bypassing any need to use the PropertyConfigurator.

the details can be found in http://logging.apache.org/log4j/docs/manual.html, search for "Default Initialization Procedure",

but a quick example would be:

java -Dlog4j.configuration=file:./log4j.properties com.test.MyTest

where "log4j.properties" is your property file (this can be either the properties file format, or the XML format).

When you say "used on a number of servers" are you implying multiple installs, or some form of distributed application - in either case, each JVM should have its own Log4J configuration, writing log out to local files. If you want to stream log information back to a single point for monitoring, there are several different types of "Appenders" that can be used for this (the parts that are configured to tell log4j how to format and output the log. Generally RollingFileAppender and ConsoleAppender are used, but there are others including SocketAppender, etc.) - we have a distributed application, spread across several JVMs on multiple servers, we just use a RollingFileAppender (actually, DailyRolling...) to store the log in a file local to each server, and use "tail -f logfile" to see the new log as its being created. We have a script that uses ssh to execute this on different servers, so from a single server we can monitor the realtime log on any other server...

Cheers,
C.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
creating threads in delphi 1 82
diffSum example 4 23
couple of eclipse 5 21
java stored proc example 9 20
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now