This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Log4J write specific event only to file  RSS feed

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my current setting (working with SLF4J)
wirite both to console and to file




i want in specific class , in specific cases write only to the lof file and not to the console,
is this possible?
 
Bartender
Posts: 20721
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have 2 options.

1. Set different reporting levels for the console and file. This is pretty common. Many times we don't want the console cluttered up with a lot of trivial messages, so we might set its log level to INFO, WARN, or higher, but the log file might also log lower-level stuff such as DEBUG or TRACE.

2. Use multiple log targets in the code. That's more trouble, but you can send specific messages to specific logs that way.
 
Marshal
Posts: 24461
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. In the system I designed back when I was working, you could write logs to a "performance" appender or to a "standard" appender . The way to choose which log to use was to prefix the class name with one of those two, like this:



(I've never seen a log4j tutorial which uses anything except the class name when creating a logger, although of course you can use any string you like for the logger name.)

And I never configured a root logger, either. There's no need for one, except that you won't find a tutorial which doesn't configure one.
 
Peter Benda
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can i,
based on the above configuration add one with TRACE level only that will write to a file?
how to set it?
 
Tim Holloway
Bartender
Posts: 20721
124
Android Eclipse IDE Java Linux Redhat Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:
(I've never seen a log4j tutorial which uses anything except the class name when creating a logger, although of course you can use any string you like for the logger name.)



Yes, it's probably a bit misleading.

Using package/class names for loggers is a generally useful idea, since it often allows setting up finer and coarser levels of logging granularity for specific subsystems and libraries. However, logger names are much more flexible than that.

For example, the logger named (something like) org.hibernate.SQL allows display of generated SQL, no matter where in the Hibernate library the sql was generated - and doesn't clutter the log with general debug information.

I've seen dual-logged code where one log might be something like "com.coderanch.message.Editor" and the same message might also be logged to "CODERANCH_MARSHALLS".

You could even have a multi-city in-house application where the logger name selected is a variable based on the user's locale and log to "PHOENIX", "TULSA", and "SIMI_VALLEY".

There's lots of options there.

As for your latest question, Peter, I'm not sure if I understand it exactly, but the logger is really 2 major parts. One is the code part where your code writes messages to one or more log channels. The other maps a log channel to an output such as console, file, email, SMS, or whatever. The master log control system allows connecting channels to one or more outputs. I'm using these term rather than their official names, because I hope that they might make things more obvious.

In any event, a given output (appender) can be defined to report events only at or above a given log severity. Thus, for example, for normal use, not record DEBUG level messages, even though the code writes them.

The opposite side of that is when you explicitly determine in application logic whether to report something based on the logger's own severity setting. For example, you might not log DEBUG if the logger's loglevel is set to INFO or higher. You would typically do that if the amount of work required to properly assemble and format a log message was significant and you didn't want to drag down application performance for something that you didn't need anyway.

Of these two options, obviously, the log level set in code is the important one, since if you never log at all (due to severity level set in code), then an appender cannot report what isn't written, no matter what its own severity level setting is.
 
Peter Benda
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still not clear,
can i add to the configuration from the first question
a new connection to dame file, but for secific class and specific debug level so i can log the data to file only?
 
That new kid is a freak. Show him this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!