aspose file tools*
The moose likes Java in General and the fly likes Conditional Logging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Conditional Logging" Watch "Conditional Logging" New topic
Author

Conditional Logging

naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
Hi all,


This is regarding conditional logging. can any one let me know what is this conditional logging.
Please provide any good tutorial link to learn from scratch level.
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
no answer in this forum thats why i tried in different forum
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14351
    
  22

Hello Naga. PatienceIsAVirtue - everybody on the forums here is a volunteer, we answer questions just because we like to help others, you don't get any guarantees that your question will be answered within an hour.

Many applications do logging, because for the developers it is often useful to know exactly what an application is doing. Especially if it is some business system, and the user of the system experiences an error or other strange behaviour of the application, then the developers need to be able to find out exactly what happened - so they use a logging framework such as Log4J or SLF4J to write log messages to a file.

Most logging frameworks have different logging levels, for example DEBUG, INFO, WARN, ERROR. In your application you log detailed information with the DEBUG level, less detail with the INFO level, things that are not supposed to happen but don't stop the application from working with the WARN level, and serious unexpected problems with the ERROR level. In the configuration of the logging framework, you can set which messages you want to see: everything, or just INFO, WARN, ERROR, or only WARN and ERROR, or only ERROR messages. The higher you set the level, the less gets logged, which keeps the log files smaller and most likely also increases the performance of the application (because it doesn't have to log all unnecessary messages).

This is called conditional logging: you only log messages if that are necessary, depending on the configuration.

Look at the websites of Log4J or SLF4J for more detailed information.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
Thank you Jesper,

actually i posted this topic in two forums. another one is blocked due to posted multiple times. for replying i have replied like that.
i can learn a lot here ans can solve my problem.

regarding conditional logging, i am referring the below link which have different levels

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/logging/Level.html. ----> i think this is not part of log4J.


can you please shed lights on this link also.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14351
    
  22

That is indeed not part of Log4J. The JDK has a built-in logging API itself, that is what you are looking at there. It is not exactly the same as Log4J, but very similar. For some reason the built-in logging API of the JDK has never become really popular, many people continue to use Log4J or SLF4J (with Logback).

The ideas about conditional logging are the same in all these logging frameworks.
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
Thankyou for your reply...

actually in my application we are using programmer defined loggers means

we have created one "logger.java". It contains methods like getInstance(), initialize(),entry(), exit(), info(), exception(), fatal() and debug().

getInstance() ---> used to initialize the only one Logger object(singleton)
initialize() ---> a files like batch.log, error.log.channelmanagement.log are created
entry() ---> this method is used when strating level processing. logs into channelmanagement.log file
exit() ---> this for batchprocess which logs into batch.log file
info() ----> this method is used for normal information. logs into channelmanagement.log file
exception() ---> this method is used when Logging is set to OFF or Logs only exceptions. logs into error.log file
fatal() ----> this method is used when only exceptions. logs into error.log file
Debug() ---> this method is used for all levels of information for debugging. logs into channelmanagement.log file


we didnot used log4j or any JDK built-in logging API. Its all defined by programmer.

to use this logs. we are using below code

private static Logger log = Logger.getInstance(1);

log.debug("xxxx");
log.info("yyyy");


here now is i can make this present code act like conditional logging means the above methods must excute based on levels
or i have to disable present code and introduce conditional logging.
Please guide me.






Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39878
    
  28
I moved this thread because I think the question is too difficult for “beginning”.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
Your logging code (not the calls to it) should handle the conditional logging functionality. For example, in your debug method the first thing that should be done is to check the currently configured logging level
Something like


Joanne
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
Thanks Joanne,

but we have problem of heam dump in one of our production server.

after analyzing, they gave some solutions. one of them is that to retrofit the existing logger and code to introduce ‘conditional’ logging.

here problem is that for example in the code log.debug("xxxxxx") .

for "xxxxxx" one String will create. if there is a code without value(for ex. " ") String will create. Due to this lots of Strings are creating.
So by using condition logging, we want to excute the particular method based on conditions i,e. if condition is true only the particular method have to execute.

I tried but its not working. please help me.

Note: as file Logger.java, we are unable to import java.util.logging.Logger as already importing the existing file with the same file name (i am not authorized to change the fle name).

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14351
    
  22

naga eswar wrote:... for "xxxxxx" one String will create. if there is a code without value(for ex. " ") String will create. Due to this lots of Strings are creating.
So by using condition logging, we want to excute the particular method based on conditions i,e. if condition is true only the particular method have to execute.

The SLF4J logging API (and its native implementation Logback) have facilities exactly to help with this problem.

If you have log statements like this:

then you indeed have a lot of overhead, because all those strings need to be concatenated to be passed to the log.debug method. Even if you have debug logging off, the strings are still concatenated, and then thrown away! That's loss of performance.

So what you can do is put checks like this everywhere where you have such log statements:

Then the string concatenation would not be done if debug logging is off.

SLF4J enables you to pass parameters like this:

That also avoids the string concatenation.
naga eswar wrote:I tried but its not working. please help me.

What exactly did you try (can you give some code examples)?
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
i am really sorry as i didnot put exact problem,

as we are using already a Logger.java(which is defined by programmer) its not allowing to import Log4j.Logger or util.Logging.Logger.

i.e,

as ther already we have import Application.common.util.Logger;
its not allowing to import import org.apache.log4j.Logger; or import import java.util.logging.Logger;

stating Application.common.util.Logger collinding with another import statement.

Actually here i want to execute programmer defined methods based conditions and that conditions should be by using log4j.Logger or logging.Logger condtional logging. Is i can do or not.
Otherwise i have to disable my current loggings and i have to introdue log4j or condtional logging. i have to update whole application which is bottle neck problem and take lots of time . Please help me.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14351
    
  22

If the only problem is that both classes are named Logger so that you can't import them both at the same time, then that's easy to solve: just import one of them, and in places where you need to use the other, specify the fully-qualified name. For example:

If it is easy to switch from your own Logger to Log4J logging depends on how the API of your own Logger works. If it is different from how you would call Log4J, then yes, you would have to update all logging statements in your code. Or you could write an adapter layer for your own logger API, so that it passes all logging on to Log4J.
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
Thats a very good idea Jesper.

Can you elaborate "adapter layer for your own logger API". How to work on this.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14351
    
  22

You just change the implementation of your own Logger class so that instead of for example writing a log message to a file, it calls Log4J, so that your own Logger class will be only a wrapper for the Log4J Logger class.
naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
thats great and good soluction.


But my team decide not to disable the current loggers. we have to provide conditions to current logger.


we have production, develpment and testing system environments.
So based on environment is we can set the logs....means

if the system is production, then only some methods in Logger.java has to execute.
if the system is development, then only someother methods in Logger.java has to execute.

The below are the methods we are using in logger


entry() ---> this method is used when strating level processing. logs into channelmanagement.log file
exit() ---> this for batchprocess which logs into batch.log file
info() ----> this method is used for normal information. logs into channelmanagement.log file
exception() ---> this method is used when Logging is set to OFF or Logs only exceptions. logs into error.log file
fatal() ----> this method is used when only exceptions. logs into error.log file
Debug() ---> this method is used for all levels of information for debugging. logs into channelmanagement.log file

if example, no need the logs called through debug() method in production environment. then we have to set this only to testing and development.
i hope we can retrieve ipadress of this system....




naga eswar
Ranch Hand

Joined: Jan 25, 2012
Posts: 120
hi everybody,

i have started working on this....

based on environment(production, development) we need to generate the logs....

in logger.java,



and at the file (probably java file), if we need to insert logs into development environment...




so here my requirement is that, in isenabled() method i dont want to hard code the ip addresses. I have to fetch that ipaddress from xml or properties file.

can you please tell me which one is prefereble, either xml or properties and how...?
 
 
subject: Conditional Logging