dataset-logger
v1.5.0
Published
DataSet NodeJS Logger
Downloads
217
Maintainers
Readme
DataSet NodeJS Logger
NodeJS DataSet Logger
Install
With NPM:
npm install dataset-logger
Or if you use Yarn:
yarn add dataset-logger
Usage
import { DataSetEventSeverity, DataSetLogger } from 'dataset-logger';
const options = {
// API Key is required.
apiKey: 'YOUR DATASET WRITE LOGS API KEY',
// SessionInfo is optional, it can be used to specify fields associated with the uploading process and
// are appended to all of your events. These fields can then be used when querying the uploaded events.
sessionInfo: {
// Should generally specify at least a `serverHost` field, containing the hostname or other server
// identifier. DataSet uses this value to organize events from different servers / sources.
serverHost: 'front-1',
serverType: 'frontend',
region: 'us-east-1',
application: 'some application name',
},
};
const logger = new DataSetLogger(options);
// Simple events can be sent by just passing a string message:
logger.log('record retrieved');
// Or more complex events can be sent like:
logger.log({
// This refers to the severity of the event
// Possible severities are `INFO`, `WARN`, `ERROR`, `DANGER`
sev: DataSetEventSeverity.INFO,
// These are the attributes of the event, and can include nested properties
attrs: {
message: 'record retrieved',
recordId: 39217,
latency: 19.4,
length: 39207,
},
});
// Once done, make sure to close the logger so any remaining events are flushed
await logger.close();
To upload unstructured logs
This package also exports a small util function that allows to upload unstructured, plain-text logs. It can be used for lightweight integrations, and to upload batches of data from stateless environments.
It uses the uploadLogs
API, it's
recommended to read more about before using it.
import { uploadLogs } from './upload-logs';
// Can receive the full string text as a parameter:
(async () => {
await uploadLogs({
apiKey: 'YOUR DATASET WRITE LOGS API KEY',
body: '{test: 123, field1: "value", field2: "value2"}',
logfile: 'some-json.log',
parser: 'json',
sessionInfo: {
serverHost: 'test-host',
region: 'us-east',
},
});
})();
// Or can also receive a file full path, which will read and send its content:
(async () => {
await uploadLogs({
apiKey: 'YOUR DATASET WRITE LOGS API KEY',
filePath: '/Users/user/some-json.log',
logfile: 'some-json.log',
parser: 'json',
sessionInfo: {
serverHost: 'test-host',
region: 'us-east',
},
});
})();
API
DataSetLogger(options?)
options
Type: object
options.apiKey (required)
Type: string
options.serverUrl (optional)
Type: `string'
options.sessionInfo (optional)
Type: object
options.shouldFlattenAttributes (optional)
Type: boolean
If nested attributes should be flatten with dot notation for easier handling when working with the events in DataSet.
The following event payload:
attrs: {
message: 'some message',
record: {
id: 'some id',
name: 'some name',
user: {
id: 'user id',
name: 'user name',
},
},
},
Would be converted to the following before sending it to DataSet:
attrs: {
message: 'some message',
'record.id': 'some id',
'record.name': 'some name',
'record.user.id': 'user id',
'record.user.name': 'user name',
},
See flatten-nested-object.spec.ts
for more cases.
options.onErrorHandler (optional)
Type: function
(error: Error) => void
options.onSuccessHandler (optional)
Type: function
(response: unknown) => void;