Re: Java Logger



On Jan 9, 8:51 am, Simon <count.numb...@xxxxxx> wrote:
Pete wrote:
On Jan 8, 9:50 pm, Simon <count.numb...@xxxxxx> wrote: I have tried to follow
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/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)

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

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



.



Relevant Pages

  • Re: A new way to configure Python logging
    ... I used that particular level for logging within a unit test ... you need to do programmatic configuration anyway. ... logger* to process incoming LogRecords. ... The Handlers will usually employ elaborate filtering, ...
    (comp.lang.python)
  • Re: A new way to configure Python logging
    ... post about your usage of logging. ... I usually register a logger 'foo' within the application and one logger ... you need to do programmatic configuration anyway. ... so that it is easy to just add multiple handlers for various levels to ...
    (comp.lang.python)
  • Re: Java Logger
    ... 12:48:20 MyLogger main ... This suggests that the ConsoleHandler has no effect on the log. ... Both the Logger and the Handlers have an associated minimum Level. ...
    (comp.lang.java.programmer)
  • Re: Java Logger
    ... I am trying to use the Logger "entering & exiting" method but with no ... propably is not enough to set the logger Level to ALL, if its Handler has ...
    (comp.lang.java.programmer)
  • Re: A new way to configure Python logging
    ... layout and how incremental logging configuration should work, ... Once implemented, the configuration format will ... I usually register a logger 'foo' within the application and one logger ... The Handlers I register have suitable Filters associated with them, ...
    (comp.lang.python)