Essentials
Logging
Logging is a service, injected and used the same way everywhere. You log against a uniform interface with levels; where those logs go, the console, a file, a cloud sink, is a configuration and a backend, not something your code decides.
#Logging from your code
Take the logger from the container and call it by level. Pass structured context as a second argument; a good backend keeps it queryable.
constructor ({ logger }) { this.logger = logger }
create (event: IncomingHttpEvent) {
const task = this.tasks.add(event.get('title'))
this.logger.info('task created', { id: task.id })
return task
}#Levels
Set the minimum level in the app options; anything below it is dropped. Use levels with intent: debug for development detail, info for notable events, warn and error for trouble.
| Level | Type | Description |
|---|---|---|
| trace | lowest | Very fine-grained tracing. |
| debug | | Development detail, off in production by default. |
| info | | Notable, expected events. |
| warn | | Something unexpected that did not stop the request. |
| error | highest | A failure worth attention. |
@StoneApp({ name: 'tasks', logger: { level: 'info' } })
export class Application {}#A custom backend
The logger is an interface; the default writes to the console. Provide your own with defineLogger to route logs to a structured sink, a file, or an observability platform, without changing a single call site.
import { defineLogger } from '@stone-js/core'
export const logger = defineLogger(({ config }) => new JsonLogger({
level: config.get('stone.logger.level', 'info')
}))