-
1. Re: logging into different log files from one class / categories
jamezp Aug 1, 2012 12:23 PM (in response to ulrich-scholz)1 of 1 people found this helpfulThe only way I can think of to do it would be to use two different loggers. Logger categories do not have to be the class name. They can be whatever you want to them to be. It's actually better practice to not use the class name as it makes it easier to configure the loggers if they use common categories.
Generally speaking loggers should be static, so two different loggers with different categories would get you what you want.
Example Logger Interface:
package org.jboss.example; import org.jboss.logging.Logger; public interface Loggers { Logger FOO_LOGGER = Logger.getLogger("foo"); Logger BAR_LOGGER = Logger.getLogger("bar"); }
Example Class:
package org.jboss.example; public class Foo { public static void main(final String[] args) { Loggers.FOO_LOGGER.info("Test from FOO_LOGGER"); Loggers.BAR_LOGGER.info("Test from BAR_LOGGER"); } }
Example config:
<file-handler name="foo-handler"> <formatter> <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/> </formatter> <file relative-to="jboss.server.log.dir" path="foo.log"/> <append value="true"/> </file-handler> <file-handler name="bar-handler"> <formatter> <pattern-formatter pattern="%d{HH:mm:ss,SSS} %-5p [%c] (%t) %s%E%n"/> </formatter> <file relative-to="jboss.server.log.dir" path="bar.log"/> <append value="true"/> </file-handler> <logger category="foo"> <level name="INFO"/> <handlers> <handler name="foo-handler"/> </handlers> </logger> <logger category="bar"> <level name="INFO"/> <handlers> <handler name="bar-handler"/> </handlers> </logger>
--
James R. Perkins
-
2. Re: logging into different log files from one class / categories
ulrich-scholz Aug 2, 2012 5:16 AM (in response to jamezp)Thanks! One more question about categories:
(I think) I have seen examples where the category filter used only part of the package name. For example, if xxx.class is my.great.project.xxx then it could have used
<logger category="my.great">
<level name="INFO"/>
<handlers>
<handler name="xxx-handler"/>
</handlers>
</logger>Is it always true that the category filter matches if it is a prefix of the category? In other words, would
<logger category="fo">
<level name="INFO"/>
<handlers>
<handler name="foo-handler"/>
</handlers>
</logger>also log the line issued by
Loggers.FOO_LOGGER.info("Test from FOO_LOGGER");
, as defined by your last post?Ulrich
-
3. Re: logging into different log files from one class / categories
jamezp Aug 2, 2012 1:25 PM (in response to ulrich-scholz)(I think) I have seen examples where the category filter used only part of the package name. For example, if xxx.class is my.great.project.xxx then it could have used
<logger category="my.great"> <level name="INFO"/> <handlers> <handler name="xxx-handler"/> </handlers> </logger>
Correct, my.great.project would use the handlers in the category my.great (parent category). However the level would not be inherited.
Is it always true that the category filter matches if it is a prefix of the category? In other words, would
<logger category="fo"> <level name="INFO"/> <handlers> <handler name="foo-handler"/> </handlers> </logger>
also log the line issued by
Loggers.FOO_LOGGER.info("Test from FOO_LOGGER");
, as defined by your last post?Think of a logger category as something like a path. In this case our path separator is a . (dot). The category gets parsed into different nodes, e.g. my.great is the parent node of my.great.project.
The logger itself is configured based on the category, but will check parent loggers for handlers. That is that when you define a logging configuration for my.great only a logger created with the exact category my.great will use that configuration. The only exception to the rule is handlers. Handlers are inherited from parent loggers unless you explicitly tell the logger not to inherit handlers.
I hope this makes sense.
--
James R. Perkins
-
4. Re: logging into different log files from one class / categories
wdfink Aug 2, 2012 2:43 PM (in response to jamezp)As James mentioned you can avoid use of parent handler by adding:
<logger category="a.b.c" use-parent-handlers="false">
Otherwise the handler of this category AND all handlers of parent are receiving such log entries.
-
5. Re: logging into different log files from one class / categories
ulrich-scholz Aug 3, 2012 3:32 AM (in response to wdfink)Do I understand correctly?
There is no explict way to assign parent handlers. The parent handler hierarchy is implictly defined via logger
categories:
- logger LA is the parent of logger LB if the category of LA is a (path-)prefix of the category of LB
- handler HA is the parent of handler HB if each of the following is true
- LA is a logger that has HA as handler
- LB is a logger that has HB as handler
- LA is the parent logger of LB
So in the example
<logger category="a.b.c">
<handlers>
<handler name="HABC"/></handlers>
<logger category="a">
<handlers>
<handler name="HA"/></handlers>
HA is the parent handler of HABC because the category a is a path-prefix of a.b.c
-
6. Re: logging into different log files from one class / categories
jamezp Aug 3, 2012 2:44 PM (in response to ulrich-scholz)Ulrich Scholz wrote:
Do I understand correctly?
There is no explict way to assign parent handlers. The parent handler hierarchy is implictly defined via logger
categories:
- logger LA is the parent of logger LB if the category of LA is a (path-)prefix of the category of LB
- handler HA is the parent of handler HB if each of the following is true
- LA is a logger that has HA as handler
- LB is a logger that has HB as handler
- LA is the parent logger of LB
So in the example
<logger category="a.b.c">
<handlers>
<handler name="HABC"/></handlers>
<logger category="a">
<handlers>
<handler name="HA"/></handlers>
HA is the parent handler of HABC because the category a is a path-prefix of a.b.c
I think you're understanding. One note though is that handlers are standalone and don't have parents or children. I know that sounds finicky, but they are different.
--
James R. Perkins
-
7. Re: logging into different log files from one class / categories
ulrich-scholz Aug 6, 2012 3:23 AM (in response to jamezp)But if handlers do not have parents, why then does the
<logger>
tag have an attributeuse-parent-handler
?Ulrich
-
8. Re: logging into different log files from one class / categories
jamezp Aug 6, 2012 10:28 AM (in response to ulrich-scholz)1 of 1 people found this helpfulThat's because the logger has parents and there could be handlers attached to those loggers. I guess I'm being kind of picky though :-). Just think of it as "use the parent loggers handlers".
--
James R. Perkins