Chapter 18. Writing Log Entries
As you write your scripts, you may want to create formal log entries for important events. Both Windows and Linux provide easy mechanisms for writing to their respective logging systems. Be sure to follow best practices when writing log entries to ensure they are useful. A good log entry has the following characteristics:
-
Uses consistent nomenclature and grammar
-
Provides context (indicating who, where, and when)
-
Is specific (indicating what)
Commands in Use
In this chapter, we introduce eventcreate and logger.
eventcreate
The eventcreate command is used in Windows environments to write entries to the event log.
Common command options
- /d
-
A detailed description of the event
- /id
-
A numeric event ID
- /l
-
The name of the event log for which to write the entry
- /so
-
The source of the event
- /t
-
The type of event
logger
The logger command is used in many Linux distributions to write events to the system log.
Common command options
- -s
-
Also write the event to stderr
- -t
-
Tag the event with the specified value
Writing Windows Logs
The eventcreate command is used to write entries to the Windows event log. In order to use it, several pieces of information must be provided:
-
Event ID (
/id): A number to identify the event. Any number between 1 and 1,000 is valid. -
Event type (
/t): A category that best describes the event. Valid options are as follows:-
ERROR -
WARNING -
INFORMATION -
SUCCESSAUDIT -
FAILUREAUDIT
-
-
Event log name ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access