TCP and UDP syslog client RFC 5424 & RFC 3164
npm install syslog-client-tlsThis module is a pure JavaScript implementation of the [BSD Syslog Protocol RFC 3164][1] and the [Syslog Protocol RFC 5424][2].
This module is installed using [node package manager (npm)][3]:
```
npm install syslog-client-tls
It is loaded using the require() function:
`js`
var syslog = require("syslog-client-tls");
TCP or UDP clients can then be created to log messages to remote hosts.
`js
var client = syslog.createClient("127.0.0.1");
client.log("example message");
`
[1]: https://www.ietf.org/rfc/rfc3164.txt
[2]: https://tools.ietf.org/html/rfc5424
[3]: https://npmjs.org
The following sections describe constants exported and used by this module.
This object contains constants for all valid values for the transportoptions
attribute passed to the argument for the createClient() function.
The following constants are defined in this object:
* TcpUdp
*
This object contains constants for all valid values for the facilityoptions
attribute passed to the argument for the log() method on theClient class. The following constants are defined in this object:
* Kernel - 0User
* - 1System
* - 3Audit
* - 13Alert
* - 14Local0
* - 16Local1
* - 17Local2
* - 18Local3
* - 19Local4
* - 20Local5
* - 21Local6
* - 22Local7
* - 23
This object contains constants for all valid values for the severityoptions
attribute passed to the argument for the log() method on theClient class. The following constants are defined in this object:
* Emergency - 0Alert
* - 1Critical
* - 2Error
* - 3Warning
* - 4Notice
* - 5Informational
* - 6Debug
* - 7
All messages are sent using an instance of the Client class. ThiscreateClient()
module exports the function which is used to createClient
instances of the class.
The createClient() function instantiates and returns an instance of theClient class:
`js
// Default options
var options = {
syslogHostname: os.hostname(),
transport: syslog.Transport.Udp,
port: 514
};
var client = syslog.createClient("127.0.0.1", options);
`
The optional target parameter defaults to 127.0.0.1. The optionaloptions parameter is an object, and can contain the following items:
* port - TCP or UDP port to send messages to, defaults to 514syslogHostname
* - Value to place into the HOSTNAME part of the HEADERos.hostname()
part of each message sent, defaults to tcpTimeout
* - Number of milliseconds to wait for a connection attempt to10000
the specified Syslog target, and the number of milliseconds to wait for
TCP acknowledgements when sending messages using the TCP transport,
defaults to (i.e. 10 seconds)transport
* - Specify the transport to use, can be eithersyslog.Transport.Udp
or syslog.Transport.Tcp, defaults tosyslog.Transport.Udp
facility
* - set default for client.log(); default is syslog.Facility.Local0.severity
* - set default for client.log(); default is syslog.Severity.Informational.rfc3164
* - set to false to use RFC 5424appName
syslog header format; default is true for the older RFC 3164
format.
* - set the APP-NAME field when using rfc5424; default uses process.titledateFormatter
* - change the default date formatter when using rfc5424; interface: function(date) { return string; }; defaults to function(date) { return date.toISOString(); }
The close event is emitted by the client when the clients underlying TCP or
UDP socket is closed.
No arguments are passed to the callback.
The following example prints a message to the console when a clients
underlying TCP or UDP socket is closed:
`js`
client.on("close", function () {
console.log("socket closed");
});
The error event is emitted by the client when the clients underlying TCP or
UDP socket emits an error.
The following arguments will be passed to the callback function:
* error - An instance of the Error class, the exposed message attribute
will contain a detailed error message.
The following example prints a message to the console when an error occurs
with a clients underlying TCP or UDP socket:
`js`
client.on("error", function (error) {
console.error(error);
});
The close() method closes the clients underlying TCP or UDP socket. Thisclose
will result in the event being emitted by the clients underlying TCPclose
or UDP socket which is passed through to the client, resulting in the client
also emitting a event.
The following example closes a clients underlying TCP or UDP socket:
`js`
client.close();
The log() method sends a Syslog message to a remote host.
The message parameter is a string containing the message to be logged.
The optional options parameter is an object, and can contain the following
items:
* facility - Either one of the constants defined in the syslog.Facilitysyslog.Facility.Local0
object or the facility number to use for the message, defaults to
(see syslog.createClient())severity
* - Either one of the constants defined in the syslog.Severitysyslog.Severity.Informational
object or the severity number to use for the message, defaults to
(see syslog.createClient())rfc3164
* - set to false to use RFC 5424timestamp
syslog header format; default is true for the older RFC 3164
format.
* - Optional Javascript Date() object to back-date the message.msgid
* - Optional RFC 5424 message-id.
The callback function is called once the message has been sent to the remotecallback
host, or an error occurred. The following arguments will be passed to the function:
* error - Instance of the Error class or a sub-class, or null if no
error occurred
Each message sent to the remote host will have a newline character appended
to it, if one is not already appended. Care should be taken to ensure newline
characters are not embedded within the message passed to this method (i.e. not
appearing at the end), as this may cause some syslog relays/servers to
incorrectly parse the message.
The following example sends a message to a remote host:
`js
var options = {
facility: syslog.Facility.Daemon,
severity: syslog.Severity.Critical
};
var message "something is wrong with this daemon!";
client.log(message, options, function(error) {
if (error) {
console.error(error);
} else {
console.log("sent message successfully");
}
});
`
Example programs are included under the modules example directory.
Tests can be run with:
``
npm test
Install dev dependencies before running test coverage:
``
npm install --dev
npm run coverage
Coverage should be generated into coverage/lcov-report/index.html.
* Initial release
* Correct typo in README.md
* Correct typo in README.md :(
* Correct typo in README.md :( :(
* Transport error events are not propagated to an error event in the Syslog
client
* Redundant release
* Slight formatting error in the README.md file
* Remove debug console.dir() statement accidently left in code
* Variable key in _expandConstantObject() missing var declaration
* Added mocha test framework
* Added istanbul test coverage
* Added tests for aprox 89% coverage
* Fixed bug where transports where not being reused
* Fixed bug where some connections would not close()options
* Made in .log() optional as per existing documentationcb
* Make in .log() optional and update documentationerror
* Fixed bug where event and .log callback wouldn't predictably receive errorclose
* event is now always fired when .close()` is called, regarless of open connection
* New maintainer Paul Grove
* Updated License
* Travis-CI and codeclimate build automation and badges
* Code linted using eslint
* No changes, issues with publishing to npm
* Fix miscalculation of PRI for Emegency and Kernel Facitilty/Severity
* Fix issue resolving IP class from hostname
* Call log callback asynchronously, preventing issues when closing in that callback
* Support for RFC 5424
* Fix erroneous space after PRI
* SirWumpus (github)
* acanimal (github)
* cdscott (github)
* mccarthy (github)
* MarkHerhold (github)
* JeremyBernier (github)
Copyright (c) 2017 Paul Grove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.