-
Notifications
You must be signed in to change notification settings - Fork 5
Logging Configuration
Nishant Aanjaney Jalan edited this page Mar 24, 2024
·
6 revisions
Minimally, there are 3 steps to create your first logger.
- Create a
Logestic
instance. - Define the parameters you wish to
use
. -
Format
the string given the parameters [^1].
Putting the 3 steps together, you get:
import { Logestic } from 'logestic';
let logger = new Logestic() // Step 1
logger = logger.use(['method', 'path']) // Step 2
const plugin = logger.format({ // Step 3
onSuccess({ method, path }) {
return `${method} ${path} was called and handled without server error.`;
},
onFailure({ request, error, code }) {
return `Oops, ${error} was thrown with code: ${code}`;
}
});
// or
const plugin = new Logestic()
.use(['method', 'path'])
.format({
onSuccess({ method, path }) {
return `${method} ${path} was called and handled without server error.`;
},
onFailure({ request, error, code }) {
return `Oops, ${error} was thrown with code: ${code}`;
}
});
plugin
is now an instance of Elysia with a logestic
object decorated in Elysia's context for explicit logging. This is great when you want consistency in your log messages[^2].
const app = new Elysia()
.use(plugin)
.get('/', ({ logestic }) => {
logestic.debug('Why is it not working?');
return 'you didn't call listen(3000)';
});
[^1]: More on formatting here.
[^2]: HttpLogging are logs recorded on every request. ExplicitLogging are logs that you specify inside a handler. Read more here