• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logging utility class  RSS feed

 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made a logging class as the product for which I am building software doesn't provide me support for logging libraries.
Below is the class I have written(which should support java5). Although I have fulfilled all my requirements from this, as there is always a way possible to improve the code, I want some guidance.

To use this logger class, one have to create instance of OdiRNLogger and pass it as an argument where ever it is needed.


 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand, why can't you use proper logging libraries?
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the tool I have to deploy my code on, is already using log4j and I want to use my log4j.properties file(so that the tool's logging doesn't get affected and I can create files where ever I want to). Also, I have to set logging level at runtime.
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, hasn't Java 5 passed End-of-Life? Not that Java 5 lacked logging support.
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I don't understand, why can't you use proper logging libraries?

I don't want to write such code
To remove such ugly code, I can use slf4j, but again, slf4j doesn't allow me to set log level at runtime.
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Also, hasn't Java 5 passed End-of-Life? Not that Java 5 lacked logging support.

Still, there are some Products/tools which(older version of them) doesn't allow later java version. one example is Oracle Data Integrator. And customer still uses them.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating your own logging library is a very brittle solution. Why is it not an option for you to add categories to the log4j configuration file? log4j also supports setting log levels at runtime.
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Creating your own logging library is a very brittle solution. Why is it not an option for you to add categories to the log4j configuration file? log4j also supports setting log levels at runtime.


And you shouldn't be talking to slf4j directly. The slf4j subsystem is a log integrator. You set your log levels using the primary app logger interface (for example, log4j). And all of the common loggers allow adjusting log levels dynamically at runtime, including, as Stephan noted, log4j.

Ugly code like

is not always necessary and in fact I don't do that in most cases. It's sufficient to code

The overhead for the logger.debug() call and its relatives logger.warning(), logger.fatal(), etc. is extremely low - micro- or even nano-seconds. Only if you are doing significant work (such as assembling complex log messages by stringing stuff together) is it necessary to check the log level, and that's just to allow bypassing the overhead of useless calculation.
 
Marco Behler
Author
Ranch Hand
Posts: 93
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To remove such ugly code, I can use slf4j, but again, slf4j doesn't allow me to set log level at runtime.


errm, slf4j is only an interface? you can use any logging library you want with it.

Yup, to second the others, it sounds fishy to build your own or not try to configure your way out with categories etc.
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marco Behler wrote:
To remove such ugly code, I can use slf4j, but again, slf4j doesn't allow me to set log level at runtime.


errm, slf4j is only an interface? you can use any logging library you want with it.


Actually, like I said, it's a log aggregator. It exists because everyone has their own preferred logging service, but in a big project you may be pulling in a lot of 3d-party stuff. Some of which logs using JULI, some of which logs using log4j, some of which logs using apache common logging, and so on.

Rather than expect everyone to adopt the same logging standard or alternatively to produce a half-dozen different copies of their projects (one for each brand of logger), the slf4j component provides a Single Log Facility for Java. It acts as backend for all those other logging APIs, then channels them out to whatever log API the application developer prefers. Unless you are inventing your own logger, you'd never actually use sl4j APIs at all.
 
Marco Behler
Author
Ranch Hand
Posts: 93
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@tim, not disagreeing there at all. i might have phrased my answer in the wrong way ;)
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Unless you are inventing your own logger, you'd never actually use sl4j APIs at all.

I don't really understand this. What's wrong, for instance, with having a compile time dependency on slf4j and using its API to perform logging, and then plugging in a library such as Logback at runtime?
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I don't really understand this. What's wrong, for instance, with having a compile time dependency on slf4j and using its API to perform logging, and then plugging in a library such as Logback at runtime?
yes I also agree with you. Using slf4j at compile time and log4j or lockback at runtime give us the power to switch between the logging frameworks easily.
 
Puspender Tanwar
Ranch Hand
Posts: 499
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a general question: Are the logging frameworks free to use or do we need to buy a licence of them?
 
Marco Behler
Author
Ranch Hand
Posts: 93
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, all the popular logging frameworks (logback, log4j etc.) are all free.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!