Java Logger

P

Pete

Hi
I am trying to use the Logger "entering & exiting" method but with no
joy.
Below is the simple program that I am running under Eclipse and get
the following output in the console tab.

08-Jan-2009 19:43:06 MyLogger main
WARNING: My first log

Can you explain why I am not getting anu output for the calls to
entering and exiting please.


Thank You.

Pete


import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MyLogger {
public static void main(String[] args) {

Logger logger = Logger.getLogger("MyLog");
logger.setLevel(Level.ALL);
logger.entering("MyLogger", "main");

logger.log(Level.WARNING, "My first log");

logger.exiting("MyLogger", "main");

}
}
 
D

Donkey Hottie

Hi
I am trying to use the Logger "entering & exiting" method but with no
joy.
Below is the simple program that I am running under Eclipse and get
the following output in the console tab.

08-Jan-2009 19:43:06 MyLogger main
WARNING: My first log

Can you explain why I am not getting anu output for the calls to
entering and exiting please.


Thank You.

Pete


import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MyLogger {
public static void main(String[] args) {

Logger logger = Logger.getLogger("MyLog");
logger.setLevel(Level.ALL);
logger.entering("MyLogger", "main");

logger.log(Level.WARNING, "My first log");

logger.exiting("MyLogger", "main");

}
}

The default handler levels in jre\lib\logging.properties is INFO. It
propably is not enough to set the logger Level to ALL, if its Handler has
it as INFO.

Dunno. I have implemented a logging package upon java.util.logging, but I
have my own Handler which sets the Level depending on my own parameters.
 
P

Pete

(e-mail address removed):




Hi
I am trying to use the Logger "entering & exiting" method but with no
joy.
Below is the simple program that I am running under Eclipse and get
the following output in the console tab.
08-Jan-2009 19:43:06 MyLogger main
WARNING: My first log
Can you explain why I am not getting anu  output for the calls to
entering and exiting please.
Thank You.

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class MyLogger {
     public static void main(String[] args) {
          Logger logger = Logger.getLogger("MyLog");
          logger.setLevel(Level.ALL);
          logger.entering("MyLogger", "main");
          logger.log(Level.WARNING, "My first log");
          logger.exiting("MyLogger", "main");
     }
}

The default handler levels in jre\lib\logging.properties is INFO. It
propably is not enough to set the logger Level to ALL, if its Handler has
it as INFO.

Dunno. I have implemented a logging package upon java.util.logging, but I  
have my own Handler which sets the Level depending on my own parameters.- Hide quoted text -

- Show quoted text -

Hi

I modified the jre\lib\logging.properties file for ".level" from INFO
to ALL as I could see how to do via an API. However this change did
not generate any output for entering or exiting.
Any other suggestions please ?


Why should logger.log() generate output and not logger.entering() or
logger.exiting().


Pete
 
S

Simon

Pete said:
The default handler levels in jre\lib\logging.properties is INFO. It
propably is not enough to set the logger Level to ALL, if its Handler has
it as INFO.
[...]

Hi

I modified the jre\lib\logging.properties file for ".level" from INFO
to ALL as I could see how to do via an API. However this change did
not generate any output for entering or exiting.
Any other suggestions please ?

You did not do what Donkey Hottie suggested. ".level" merely affects the
loggers, not the Handlers. If you are using a console handler try
something like:

java.util.logging.ConsoleHandler.level = ALL

The java.util.logging package is quite well documented, but you have to
understand it entirely, otherwise you get confused. Start with the docs
of LogManager. The docs of the Handlers say what properties they read.

Instead of editing the system wide jre/lib/logging.properties, you can
also use your own properties file and start your app with

java -Djava.util.logging.config.file=YOURFILE
Why should logger.log() generate output and not logger.entering() or
logger.exiting().

The API documentation of Logger.entering() and Logger.exiting() answers
your question: It generates messages with a level of FINER, and you are
using WARNING whenever you use Logger.log(). User
Logger.log(Level.FINER, ...) and it won't generate output either.

Cheers,
Simon
 
P

Pete

Pete said:
The default handler levels in jre\lib\logging.properties is INFO. It
propably is not enough to set the logger Level to ALL, if its Handler has
it as INFO.
[...]



I modified the jre\lib\logging.properties file for ".level" from INFO
to ALL as I could see how to do via an API. However this change did
not generate any output for entering or exiting.
Any other suggestions please ?

You did not do what Donkey Hottie suggested. ".level" merely affects the
loggers, not the Handlers. If you are using a console handler try
something like:

  java.util.logging.ConsoleHandler.level = ALL

The java.util.logging package is quite well documented, but you have to
understand it entirely, otherwise you get confused. Start with the docs
of LogManager. The docs of the Handlers say what properties they read.

Instead of editing the system wide jre/lib/logging.properties, you can
also use your own properties file and start your app with

  java -Djava.util.logging.config.file=YO URFILE
Why should logger.log() generate output and not logger.entering() or
logger.exiting().

The API documentation of Logger.entering() and Logger.exiting() answers
your question: It generates messages with a level of FINER, and you are
using WARNING whenever you use Logger.log(). User
Logger.log(Level.FINER, ...) and it won't generate output either.

Cheers,
Simon


Simon,

I have tried to follow the documentation and like you said it is not
straight forward, certainly not for me.

All I am trying to do is get the above small program to generate
output for the "entering & exiting" calls. I am not using a console
Handler in the code, as you can see. I don't really understand this.
However, I notice in the jre\lib\logging.properties file the
following line :-

handlers= java.util.logging.ConsoleHandler

Your suggestions of
java.util.logging.ConsoleHandler.level = ALL
should that go in program ? if so, the compiler complained of not
being to resolve.

Is it possible to get the small program working and making any changes
from within using appropriate calls and without needing to make
changes to files manually ?


Thanks

Pete
 
M

Mike Schilling

Pete said:
Pete said:
The default handler levels in jre\lib\logging.properties is INFO.
It propably is not enough to set the logger Level to ALL, if its
Handler has it as INFO.
[...]



I modified the jre\lib\logging.properties file for ".level" from
INFO to ALL as I could see how to do via an API. However this
change did not generate any output for entering or exiting.
Any other suggestions please ?

You did not do what Donkey Hottie suggested. ".level" merely
affects
the loggers, not the Handlers. If you are using a console handler
try
something like:

java.util.logging.ConsoleHandler.level = ALL

The java.util.logging package is quite well documented, but you
have
to understand it entirely, otherwise you get confused. Start with
the docs of LogManager. The docs of the Handlers say what
properties
they read.

Instead of editing the system wide jre/lib/logging.properties, you
can also use your own properties file and start your app with

java -Djava.util.logging.config.file=YO URFILE
Why should logger.log() generate output and not logger.entering()
or
logger.exiting().

The API documentation of Logger.entering() and Logger.exiting()
answers your question: It generates messages with a level of FINER,
and you are using WARNING whenever you use Logger.log(). User
Logger.log(Level.FINER, ...) and it won't generate output either.

Cheers,
Simon


Simon,

I have tried to follow the documentation and like you said it is not
straight forward, certainly not for me.

All I am trying to do is get the above small program to generate
output for the "entering & exiting" calls. I am not using a console
Handler in the code, as you can see. I don't really understand this.
However, I notice in the jre\lib\logging.properties file the
following line :-

handlers= java.util.logging.ConsoleHandler

Your suggestions of
java.util.logging.ConsoleHandler.level = ALL
should that go in program ? if so, the compiler complained of not
being to resolve.

Is it possible to get the small program working and making any
changes
from within using appropriate calls and without needing to make
changes to files manually ?

Sure. Call getHandlers() on your Logger.. This will return an array
of Handlers, one of which should be a ConsoleHandler. Call
setLevel(Level.ALL) on it.
 
K

Knute Johnson

Pete said:
Pete said:
The default handler levels in jre\lib\logging.properties is INFO. It
propably is not enough to set the logger Level to ALL, if its Handler has
it as INFO. [...]



Hi
I modified the jre\lib\logging.properties file for ".level" from INFO
to ALL as I could see how to do via an API. However this change did
not generate any output for entering or exiting.
Any other suggestions please ?
You did not do what Donkey Hottie suggested. ".level" merely affects the
loggers, not the Handlers. If you are using a console handler try
something like:

java.util.logging.ConsoleHandler.level = ALL

The java.util.logging package is quite well documented, but you have to
understand it entirely, otherwise you get confused. Start with the docs
of LogManager. The docs of the Handlers say what properties they read.

Instead of editing the system wide jre/lib/logging.properties, you can
also use your own properties file and start your app with

java -Djava.util.logging.config.file=YO URFILE
Why should logger.log() generate output and not logger.entering() or
logger.exiting().
The API documentation of Logger.entering() and Logger.exiting() answers
your question: It generates messages with a level of FINER, and you are
using WARNING whenever you use Logger.log(). User
Logger.log(Level.FINER, ...) and it won't generate output either.

Cheers,
Simon


Simon,

I have tried to follow the documentation and like you said it is not
straight forward, certainly not for me.

All I am trying to do is get the above small program to generate
output for the "entering & exiting" calls. I am not using a console
Handler in the code, as you can see. I don't really understand this.
However, I notice in the jre\lib\logging.properties file the
following line :-

handlers= java.util.logging.ConsoleHandler

Your suggestions of
java.util.logging.ConsoleHandler.level = ALL
should that go in program ? if so, the compiler complained of not
being to resolve.

Is it possible to get the small program working and making any changes
from within using appropriate calls and without needing to make
changes to files manually ?


Thanks

Pete

Pete:

Any top level logger you create has a parent called root and root has a
Handler called ConsoleHandler. What you are seeing is the output from
the ConsoleHandler. The default level for the handler is something less
than finer which is why you aren't seeing the exit. I modified your
program to get the parent logger, its handler and set the log level on
that handler to ALL. One important thing to remember is that the Logger
does nothing with the messages, it takes a Handler to do something with
them and they both have levels.

And I have to agree with you that it is very confusing at first (even at
second).

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.Handler; // added
import java.util.logging.SimpleFormatter;

public class MyLogger {
public static void main(String[] args) {

Logger logger = Logger.getLogger("MyLog");

Logger parent = logger.getParent();
Handler[] handlers = parent.getHandlers();
for (Handler handler : handlers) {
System.out.println(handler);
handler.setLevel(Level.ALL);
}

logger.setLevel(Level.ALL);
logger.entering("MyLogger", "main");

logger.log(Level.WARNING, "My first log");

logger.exiting("MyLogger", "main");
try {
Thread.sleep(1000);
} catch (InterruptedException ie) { }

}
}
 
S

Simon

Pete said:
the documentation and like you said it is not straight forward, certainly not
for me.

You are right, It's tricky.
All I am trying to do is get the above small program to generate output for
the "entering & exiting" calls. I am not using a console Handler in the code,
as you can see.

You might be using it without knowing, though this is not defined "in the code",
but, as you found out yourself, here:
I don't really understand this. However, I notice in the
jre\lib\logging.properties file the following line :-

handlers= java.util.logging.ConsoleHandler


Your suggestions of java.util.logging.ConsoleHandler.level = ALL should that
go in program ? if so, the compiler complained of not being to resolve.

No, it is not Java code. It goes into the logging.properties file. Every Handler
uses its own set of properties. E.g. for the ConsoleHandler, read

http://java.sun.com/javase/6/docs/api/java/util/logging/ConsoleHandler.html

It has the same effect as the solution posted by Knute and Mike.



If you decide to make these settings programmatically in this way, note that
this prevents the user from customizing the logging behaviour. One way out would
be to go with a properties file which you specify e.g. on the command line as I
suggested in my previous posting. If you don't want to do that, you can still do
it programmatically by using the following mechanism (taken from
http://java.sun.com/javase/6/docs/api/java/util/logging/LogManager.html)
If the "java.util.logging.config.class" property is set, then the property
value is treated as a class name. The given class will be loaded, an object
will be instantiated, and that object's constructor is responsible for
reading in the initial configuration. (That object may use other system
properties to control its configuration.) The alternate configuration class
can use readConfiguration(InputStream) to define properties in the
LogManager.

In other words, copy the code posted by Knute to the constructor of a class, say
MyLoggingConfig, point the said property to this class, e.g. using the -D switch
of java:

java -Djava.util.logging.config.class=MyLoggingConfig MainClass

Not particularly beautiful to trigger side effects from a constructor, though,
but it leaves the user the possibility to customize logging.

Cheers,
Simon
 
P

Pete

You are right, It's tricky.


You might be using it without knowing, though this is not defined "in the code",
but, as you found out yourself, here:



No, it is not Java code. It goes into the logging.properties file. Every Handler
uses its own set of properties. E.g. for the ConsoleHandler, read

http://java.sun.com/javase/6/docs/api/java/util/logging/ConsoleHandle...

It has the same effect as the solution posted by Knute and Mike.

If you decide to make these settings programmatically in this way, note that
this prevents the user from customizing the logging behaviour. One way out would
be to go with a properties file which you specify e.g. on the command line as I
suggested in my previous posting. If you don't want to do that, you can still do
it programmatically by using the following mechanism (taken fromhttp://java.sun.com/javase/6/docs/api/java/util/logging/LogManager.html)


In other words, copy the code posted by Knute to the constructor of a class, say
MyLoggingConfig, point the said property to this class, e.g. using the -D switch
of java:

  java -Djava.util.logging.config.class=MyLoggingConfig MainClass

Not particularly beautiful to trigger side effects from a constructor, though,
but it leaves the user the possibility to customize logging.

Cheers,
Simon

Gents,

Many thanks for both educational and help response. I should have said
before, why I was interested in the Logger class. I am doing my
assignment for the SCJD exam and using Andrew Monkhouse's book which
an example project in it. You may be aware of this. It uses the Logger
class in it. So I decided to understand the working of the Logger
using what I thought was a simple program, but not aware that this
simple program use the "jre\lib\logging.properties" file which
affected it.

There is no requirement for the SCJD to use the Logger in the
assignment. So for me to use it, would be used via the properties and
not introduce any Logger specific code in the assignment - except for
the log methods ( log, entering, exiting).

Simon. would you be able to send me a example config file as you
suggested in your initial response. Presumably this will specific for
the ConsoleHandler.

I still have one questions, in my orginal program, logger.log()
generated output however not logger.entering() or logger.exiting().
This suggests that the ConsoleHandler has no effect on the log(). Is
this correct ?

Once again many thanks.


Pete
 
S

Simon

Simon. would you be able to send me a example config file as you
suggested in your initial response. Presumably this will specific for
the ConsoleHandler.

I can try. Your Logger's name is "MyLog" in your original code.

[my_logging.properties]
MyLog.handlers = java.util.logging.ConsoleHandler
MyLog.level = ALL
java.util.logging.ConsoleHandler.level = ALL
[/my.logging.properties]

This yields (using the code from your initial post)

$ java MyLogger
09.01.2009 12:48:20 MyLogger main
WARNING: My first log

$ java -Djava.util.logging.config.file=my_logging.properties MyLogger
09.01.2009 12:47:34 MyLogger main
FINER: ENTRY
09.01.2009 12:47:34 MyLogger main
WARNING: My first log
09.01.2009 12:47:34 MyLogger main
FINER: RETURN
I still have one questions, in my orginal program, logger.log()
generated output however not logger.entering() or logger.exiting().
This suggests that the ConsoleHandler has no effect on the log(). Is
this correct ?

Not in general.

Just to clarify: whether log() has an effect or not depends on its first
argument which defines the Level. Calling Logger.entering("MyClass", "myMethod")
is nothing more than calling Logger.log(Level.FINER, "MyClass", "myMethod",
"ENTRY"); so there is no real difference between log(Level.FINER) and
entering(). The first produces a result if and only if the second does.

Both the Logger and the Handlers have an associated minimum Level. Any message
below this Level will be dropped. If a message passed to one of the Logger.log()
methods has at least the effective Level, a LogRecord will be created and
published to the Handlers associated with the Logger (and to the Logger's parent
Logger). Then, the LogRecord must pass a second test since the Handlers have a
Level of their own. This is independent of whether it's a ConsoleHandler or a
FileHandler. Effectively, the message will be logged if the Level is at least
the maximum level of the Logger and the Handler.

This may be of interest:

http://java.sun.com/javase/6/docs/technotes/guides/logging/overview.html
http://java.sun.com/javase/6/docs/api/java/util/logging/package-summary.html

Cheers,
Simon
 
P

Pete

Simon. would you be able to send me a example config file as you
suggested in your initial response. Presumably this will specific for
the ConsoleHandler.

I can try. Your Logger's name is "MyLog" in your original code.

[my_logging.properties]
MyLog.handlers = java.util.logging.ConsoleHandler
MyLog.level = ALL
java.util.logging.ConsoleHandler.level = ALL
[/my.logging.properties]

This yields (using the code from your initial post)

$ java MyLogger
09.01.2009 12:48:20 MyLogger main
WARNING: My first log

$ java -Djava.util.logging.config.file=my_logging.properties MyLogger
09.01.2009 12:47:34 MyLogger main
FINER: ENTRY
09.01.2009 12:47:34 MyLogger main
WARNING: My first log
09.01.2009 12:47:34 MyLogger main
FINER: RETURN
I still have one questions, in my orginal program, logger.log()
generated output however not logger.entering() or logger.exiting().
This suggests that the ConsoleHandler has no effect on the log(). Is
this correct ?

Not in general.

Just to clarify: whether log() has an effect or not depends on its first
argument which defines the Level. Calling Logger.entering("MyClass", "myMethod")
is nothing more than calling Logger.log(Level.FINER, "MyClass", "myMethod",
"ENTRY"); so there is no real difference between log(Level.FINER) and
entering(). The first produces a result if and only if the second does.

Both the Logger and the Handlers have an associated minimum Level. Any message
below this Level will be dropped. If a message passed to one of the Logger.log()
methods has at least the effective Level, a LogRecord will be created and
published to the Handlers associated with the Logger (and to the Logger's parent
Logger). Then, the LogRecord must pass a second test since the Handlers have a
Level of their own. This is independent of whether it's a ConsoleHandler or a
FileHandler. Effectively, the message will be logged if the Level is at least
the maximum level of the Logger and the Handler.

This may be of interest:

http://java.sun.com/javase/6/docs/t...se/6/docs/api/java/util/logging/package-summa...

Cheers,
Simon


Hi Simon.

That was a super quick response and one I think gives me sufficient
information to go forward with my assignment. Thank you.

I don't want to push my luck but I do currently have a few questions
w.r.t my SCJD assignment on the subject RMI.
Would you like to help me that? I am very happy with your help w.r.t
the Logger, so if you decline.. that is no problem.

Pete
 
S

Simon

That was a super quick response and one I think gives me sufficient
information to go forward with my assignment. Thank you.

No worries.
I don't want to push my luck but I do currently have a few questions
w.r.t my SCJD assignment on the subject RMI.

I haven't used RMI for quite some time. Post your question and see if someone
answers :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top