Rules and advice for logging?
Posted
by
Nick Rosencrantz
on Programmers
See other posts from Programmers
or by Nick Rosencrantz
Published on 2012-09-21T10:50:20Z
Indexed on
2012/09/21
15:52 UTC
Read the original article
Hit count: 432
In my organization we've put together some rules / guildelines about logging that I would like to know if you can add to or comment.
We use Java but you may comment in general about loggin - rules and advice
Use the correct logging level
- ERROR: Something has gone very wrong and need fixing immediately
- WARNING: The process can continue without fixing. The application should tolerate this level but the warning should always get investigated.
- INFO: Information that an important process is finished
- DEBUG. Is only used during development
Make sure that you know what you're logging.
Avoid that the logging influences the behavior of the application
The function of the logging should be to write messages in the log.
- Log messages should be descriptive, clear, short and concise.
There is not much use of a nonsense message when troubleshooting.
- Put the right properties in log4j
Put in that the right method and class is written automatically.
Example:
Datedfile -web
log4j.rootLogger=ERROR, DATEDFILE
log4j.logger.org.springframework=INFO
log4j.logger.waffle=ERROR
log4j.logger.se.prv=INFO
log4j.logger.se.prv.common.mvc=INFO
log4j.logger.se.prv.omklassning=DEBUG
log4j.appender.DATEDFILE=biz.minaret.log4j.DatedFileAppender
log4j.appender.DATEDFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.DATEDFILE.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%C{1}.%M] - %m%n
log4j.appender.DATEDFILE.Prefix=omklassning.
log4j.appender.DATEDFILE.Suffix=.log
log4j.appender.DATEDFILE.Directory=//localhost/WebSphereLog/omklassning/
- Log value.
Please log values from the application.
- Log prefix.
State which part of the application it is that the logging is written from, preferably with something for the project agreed prefix e.g. PANDORA_DB
- The amount of text.
Be careful so that there is not too much logging text. It can influence the performance of the app.
- Loggning format:
-There are several variants and methods to use with log4j but we would like a uniform use of the following format, when we log at exceptions:
logger.error("PANDORA_DB2: Fel vid hämtning av frist i
TP210_RAPPORTFRIST", e);
In the example above it is assumed that we have set log4j properties so that it automatically write the class and the method.
Always use logger and not the following:
System.out.println(), System.err.println(), e.printStackTrace()
If the web app uses our framework you can get very detailed error information from EJB, if using try-catch in the handler and logging according to the model above:
In our project we use this conversion pattern with which method and class names are written out automatically . Here we use two different pattents for console and for datedfileappender:
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.appender.DATEDFILE.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
In both the examples above method and class wioll be written out. In the console row number will also be written our.
toString()
Please have a toString()
for every object.
EX:
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(" DwfInformation [ ");
sb.append("cc: ").append(cc);
sb.append("pn: ").append(pn);
sb.append("kc: ").append(kc);
sb.append("numberOfPages: ").append(numberOfPages);
sb.append("publicationDate: ").append(publicationDate);
sb.append("version: ").append(version);
sb.append(" ]");
return sb.toString();
}
instead of special method which make these outputs
public void printAll()
{
logger.info("inbet: " + getInbetInput());
logger.info("betdat: " + betdat);
logger.info("betid: " + betid);
logger.info("send: " + send);
logger.info("appr: " + appr);
logger.info("rereg: " + rereg);
logger.info("NY: " + ny);
logger.info("CNT: " + cnt);
}
So is there anything you can add, comment or find questionable with these ways of using the logging? Feel free to answer or comment even if it is not related to Java, Java and log4j is just an implementation of how this is reasoned.
© Programmers or respective owner