Solved

Need logging object available to classes in my package

Posted on 2004-10-13
8
187 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
runtime exception 2 42
Tvertscrollbox like a whatsapp layout 5 36
sql import cannot be resolved jsp 3 49
Fast way to search item into Java Array (Rhino compatible) 2 31
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

828 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