@netsells/netsells-logger-adonis
v1.4.1
Published
This module wraps the `@netsells/netsells-logger-node` package in an Adonis Service Provider for easy integration into Adonis projects. This module will also provide a `/__logger` route in your application, which can receive a `POST` with a JSON payload o
Downloads
2
Keywords
Readme
Netsells Logger Adonis Provider
This module wraps the @netsells/netsells-logger-node
package in an Adonis Service Provider for easy integration into Adonis projects. This module will also provide a /__logger
route in your application, which can receive a POST
with a JSON payload of severity
and body
. For further info, check out the Nuxt Integration.
Installation
yarn add @netsells/netsells-logger-adonis
Add the following line to your Adonis providers in start/app.js
:
const providers = [
'@netsells/netsells-logger-adonis/providers/NetsellsLoggerProvider',
];
To set the logger as your applications default, update the default logger transport in your Adonis application config:
module.exports = {
logger: {
transport: 'netsells',
},
};
Also add a new config entry for the Netsells transport below:
module.exports = {
logger: {
netsells: {
driver: 'netsells',
name: 'application-name',
// This is retrieved from the APP_ENV variable if set,
// but you can override here with one of the following
// options as a baseline: 'local', 'staging', 'production'
environment: 'production',
// Netsells logger config values
},
},
};
Usage
This module will simply enable a new logger transport in your application, so can be utilised in the same way as normal.
const Logger = use('Logger');
Logger.info('Some information');
Logger.warning('Some information');
Logger.error('Some information');
Logger.debug('Some information');
Check out the Logger Documentation for more details.
The second parameter can be set to pass additional data through to the logger instance. This format matches the format outlined in the Logging Docs.
// You can pass exceptions directly and they will be parsed for you
try {
throw new Error('Some application logic error');
} catch (e) {
Logger.error('An error was thrown in application logic', {
exception: e,
});
}
// You can also add additional context data
Logger.error('An error was thrown in application logic', {
exception: e,
context: {
formData: request.all(),
},
});
Config
The following values can be set:
name
- Type:
string
- Default:
'adonis-app'
The application name that logs should be grouped by. This should be changed.
level
- Type:
string
- Default:
'info'
The default logging level.
directory
- Type:
string
- Default:
{appRoot}/logs
The directory that log files should be stored in. This should be an absolute path.
component
- Type:
string
- Default:
'frontend'
The component to group logs by.
subComponent
- Type:
string
- Default:
'node'
The sub-component to group logs by.
Integrations
This package also provides some integrations for frontend frameworks.
Nuxt
To enable a few helper methods in your application to communicate with your server-side logger, you can register the module in your nuxt config:
module.exports = {
modules: [
'@netsells/netsells-logger-adonis/nuxt',
],
};
The following methods are then available to be used through your application:
// Generic log
this.$log('message', 'severity');
// Severity must be one of:
// emerg, alert, crit, error, warning, notice, info, debug
// You can add an optional logger object as the second parameter if needed
this.$log('message', {
// Logger data
}, 'severity');
// Error shorthand
this.$log('message', {
// optional log object
});
// Info shorthand
this.$logInfo('message', {
// optional log object
});
Ensure you allow the /__logger
endpoint to be accessed via POST without csrf protection by updating your csrf.filterUris
value in your shield.js
config in Adonis, e.g.:
module.exports = {
csrf: {
enable: true,
filterUris: [
'/__logger',
],
},
};
Example usage
async login() {
try {
await this.$auth.login();
} catch (e) {
this.$logError('An error occurred when logging in', {
exception: e,
// This options object follows the same format
// as the server-side component
});
}
},