Getting Started
LogLayer is designed to work seamlessly across both server-side and browser environments. However, individual transports and plugins may have specific environment requirements, which is indicated on their respective page.
Installation
Node.js
npm install loglayerpnpm add loglayeryarn add loglayerDeno
For Deno, you can use npm: specifiers or import maps:
Using npm: specifiers:
import { LogLayer } from "npm:loglayer@latest";Using import maps (recommended):
// deno.json
{
"imports": {
"loglayer": "npm:loglayer@latest"
}
}// main.ts
import { LogLayer } from "loglayer";For detailed Deno setup and examples, see the Deno integration guide.
Bun
For Bun, you can install LogLayer using bun's package manager:
bun add loglayerFor detailed Bun setup and examples, see the Bun integration guide.
Basic Usage with Console Transport Browser Server
The simplest way to get started is to use the built-in console transport, which uses the standard console object for logging:
import { LogLayer, ConsoleTransport } from 'loglayer'
const log = new LogLayer({
transport: new ConsoleTransport({
logger: console,
}),
})
// Basic logging
log.info('Hello world!')
// Logging with metadata
log.withMetadata({ user: 'john' }).info('User logged in')
// Logging with context (persists across log calls)
log.withContext({ requestId: '123' })
log.info('Processing request') // Will include requestId
// Logging errors
log.withError(new Error('Something went wrong')).error('Failed to process request')Structured logging
If you want to use the Console Transport as a structured logger (JSON output), see the Console Transport structured logging section.
Using an Error Serializer
When logging errors, JavaScript Error objects don't serialize to JSON well by default. We recommend using an error serializer like serialize-error to ensure error details are properly captured:
npm install serialize-errorpnpm add serialize-erroryarn add serialize-errorimport { LogLayer, ConsoleTransport } from 'loglayer'
import { serializeError } from 'serialize-error'
const log = new LogLayer({
errorSerializer: serializeError,
transport: new ConsoleTransport({
logger: console,
}),
})
// Error details will be properly serialized
log.withError(new Error('Something went wrong')).error('Failed to process request')For more error handling options, see the Error Handling documentation.
Next steps
- Optionally configure LogLayer to further customize logging behavior.
- See the Console Transport documentation for more configuration options.
- Start exploring the Logging API section for more advanced logging features.
- See the Transports section for more ways to ship logs to different destinations.
