Splunk logging for JavaScript
See the Splunk HEC Docs for more info.
TODO:
- Test on an actual Splunk server.
npm install 'kersplunk'
Create a logger singleton (recommended for most applications):
import { Logger } from 'kersplunk';
export const logger = Logger.singleton({
splunkUrl: 'http://my-splunk/http-event-collector-path',
authToken: 'YOUR-SPLUNK-HEC-TOKEN'
}):
Then, just use the logger around your application:
import { logger } from './utils/logger';
// Log away!
logger.info('thing:happened', {whatever: 'details', you: 'would', like: { to: 'add'}});
Each log entry is tagged with a logType
property. These are intended to be broad categories of logs. You have control over the log types the logger can create.
By default, loggers will have:
info
- Informationaldebug
- Debugging level stuff (may only want this in your lower environments)warn
- Warnings (usually for recoverable errors)error
- Hard exceptions
You may scrap these and create your own set by passing in your logger types into the singleton
or create
methods.
const myLogger = Logger.create(config, 'happy', 'sad');
myLogger.happy('Wooo!! 😀'); // -> {logType: 'happy', eventName: 'Wooo!! 😀'}
myLogger.sad('Booooo ☹️'); // -> {logType: 'sad', eventName: 'Booooo ☹️'}
The logger will combine all the information about your event into a single entry.
For example, logging this:
logger.debug('it worked!', {note: 'I am awesome', foo: ['bar', 'baz']});
will create a log entry in Splunk like this:
{
logType: 'debug',
eventName: 'it worked!',
note: 'I am awesome',
foo: ['bar', 'baz']
}
| Name | Type | Default | Notes |
|--|--|--|
| splunkUrl | string
| undefined
| The URL to your Splunk HEC Collector endpoint |
| authToken | string
| undefined
| Your Splunk HEC token |
| interceptor | function
| undefined
| Allows for adding common log properties globally |
| enabled | boolean
| true | enable/disable the logger |
|TODO| FINISH| THIS TABLE|
This is the recommended way to use the logger for most projects
Creates a "singleton" logger instance. The intention is that for a given JavaScript process, only one logger will ever be created by this method. This is useful if you would like to configure your logger once in your application and all modules will receive the same Logger
instance.
Creates a new logger instance.