Java Reference
In-Depth Information
or a server error, so it isn't written to the error log. In some situations, you might want
to log it in the audit log, or a third location. However, remember the golden rule of
logging: if no one's going to look at it, don't log it. Unless you really plan to investigate
and do something about client disconnects, don't bother to record them.
By default, the logs are just output to the console. For example, here's the output from
the preceding server when I connected to it a few times in quick succession:
Apr 13, 2013 8:54:50 AM LoggingDaytimeServer$DaytimeTask call
INFO: Sat Apr 13 08:54:50 EDT 2013 /0:0:0:0:0:0:0:1:56665
Apr 13, 2013 8:55:08 AM LoggingDaytimeServer$DaytimeTask call
INFO: Sat Apr 13 08:55:08 EDT 2013 /0:0:0:0:0:0:0:1:56666
Apr 13, 2013 8:55:16 AM LoggingDaytimeServer$DaytimeTask call
INFO: Sat Apr 13 08:55:16 EDT 2013 /0:0:0:0:0:0:0:1:56667
You'll want to configure the runtime environment such that logs go to a more permanent
destination. Although you can specify this in code, it's usually advisable to set this up
in a configuration file so log locations can be changed without recompiling.
The java.util.logging.config.file system property points to a file in the normal
properties format that controls the logging. You set this property by passing the
-Djava.util.logging.config.file=_filename_ argument when launching the vir‐
tual machine. For instance, in Mac OS X, it might be set in the VMOptions in the In‐
fo.plist file:
< key > Java </ key >
< dict >
< key > VMOptions </ key >
< array >
< string >- Djava . util . logging . config . file =/ opt / daytime / logging . properties
</ string >
</ array >
</ dict >
Example 9-7 is a sample logging properties file that specifies:
• Logs should be written to a file.
• The requests log should be in /var/logs/daytime/requests.log at level Info.
• The errors log should be in /var/logs/daytime/requests.log at level Severe.
• Limit the log size to about 10 megabytes, then rotate.
• Keep two logs: the current one and the previous one.
• Use the basic text formatter (not XML).
• Each line of the logfile should be in the form level message timestamp .
Search WWH ::




Custom Search