Solved

Need logging object available to classes in my package

Posted on 2004-10-13
8
184 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

705 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

20 Experts available now in Live!

Get 1:1 Help Now