DailyJS

Windows and Node: Event Logs

Alex R. Young

Subscribe

@dailyjs

Facebook

Google+

tutorials node logging windows windows-and-node

Windows and Node: Event Logs

Posted by Alex R. Young on .
Featured

tutorials node logging windows windows-and-node

Windows and Node: Event Logs

Posted by Alex R. Young on .
To read previous posts in this series, view the windows-and-node tag.

Windows has event logs with three categories: System, Application, and Security. These are known as "sources" in Windows terminology, and since NT 4.0 it's been possible to create custom sources as well. Pretty much every version of Windows I've ever seen has included an Event Viewer utility, which can be used to view and manipulate logs.

Event logs are useful in Windows for the same reasons as syslog in Unix. Logging messages from background services is one of the most obvious examples, particularly as Node seems like a promising platform for developing Windows services.

In Node this requires an addon, because native bindings are needed to communicate with the eventlog service. Windows Event Log Js (GitHub: jfromaniello / windowseventlogjs, License: MIT, npm: windows-eventlog) by José F. Romaniello is one such addon.

On a related note, José has also written a great tutorial on how to create Node addons for Windows using Visual Studio, it's got detailed instructions and screenshots with full sample code: Writing your first native module for node.js on Windows.

To use this module open cmd.exe and install the module:

npm install windows-eventlog  

Now create a simple script to write some test messages:

var EventLog = require('windows-eventlog').EventLog  
  , logger = new EventLog('dailyjs-logs');

logger.log('Hello from DailyJS');  

This won't run yet, however. Start another cmd.exe, but this time right-click the icon and select "Run as administrator":

Run as administrator

Now the example above should run correctly. Open Event Viewer, then select "Windows Logs", "Application", and look for logs with the source dailyjs-logs (assuming you didn't change it). You should see something like this:

Event Viewer

If portability with existing Unix software is required, then check out José's transport for Winston that uses this module.