Re: minlog query

96 views
Skip to first unread message

Nate

unread,
Jul 27, 2010, 3:23:43 AM7/27/10
to Richards Peter, minlog...@googlegroups.com
Hi Richards,

Imagine you have this statement:
if (loggingEnabled)  System.out.println("Print this message!");

Every time this code is run, loggingEnabled has to be checked. Now if you define loggingEnabled to be "final" then this will be the code if loggingEnabled is true:
if (true) System.out.println("Print this message!");

This gets simplified by javac (the java compiler) to this:
System.out.println("Print this message!");

As you can see, no boolean check is needed because it is always true. If you define loggingEnabled to be "final" then this will be the code if loggingEnabled is false:
if (false) System.out.println("Print this message!");

This gets simplified by javac to do absolutely nothing. Because the contents of the if statement can never be executed, the if statement and its contents are discarded. MinLog uses this fact to allow you to compile with a hard coded a logging level. Eg, take these statements:
if (level <= DEBUG) System.out.println("Print this DEBUG message!");
if (level <= INFO) System.out.println("Print this INFO message!");
if (level <= WARN) System.out.println("Print this WARN message!");
if (level <= ERROR) System.out.println("Print this ERROR message!");

If you hard code MinLog so that level is final and equals WARN, then the above statements would become:
// Removed because level is always WARN which is always less than DEBUG.
// Removed because level is always INFO which is always less than INFO.
System.out.println("Print this WARN message!");
System.out.println("Print this ERROR message!");

As you can see, some code can be removed by the compiler, and some if statements can be removed.

MinLog does not have many features. It is extremely simple and only shared on Google Code so that other projects don't have to write their own identical class. I used to just include a class like MinLog for each project, until I ran into project A depending on project B, and I didn't want to make users customize logging in a different way for each project.

MinLog should only be used for very simple logging. While it is more efficient than libraries with a lot more features, the differences are likely to be negligible on desktop computers. If you need extensive logging features then don't use MinLog. If you don't mind a few boolean checks for logging (most apps are fine with this), then don't use MinLog. If you aren't sure, don't use MinLog until logging becomes a bottleneck for your code.

I mostly use MinLog in games and for libraries that are intended to be used in games, where you want to be efficient, especially with 3rd party code. MinLog would also be appropriate for an app where you need to do an extreme amount of debug level logging AND you want to avoid the logging overhead by compilng it away.

-Nate


On Mon, Jul 26, 2010 at 6:05 AM, Richards Peter <hbkri...@gmail.com> wrote:
Hi,

I am a java developer. I have just started my career as a developer. Till now me and my project team were using only log4j for all our logging activities. I have heard from some of my seniors that log4j cannot be the only solution. It was then I came to know about minlog through google codes.

1) Can u enlighten me about the added advantages that Minlog has over log4j.
2) Can you explain me what is meant by ' Logging statements below a given level can be automatically removed by javac at compile time.' I saw yourr article at http://code.google.com/p/minlog/wiki/OverheadAndComparisons. But I would be happy if you can tell me more in detail about this.

Thanks,
Richards.

Reply all
Reply to author
Forward
0 new messages