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
sh
npm install loglayersh
pnpm add loglayersh
yarn add loglayerDeno
For Deno, you can use npm: specifiers or import maps:
Using npm: specifiers:
typescript
import { LogLayer } from "npm:loglayer@latest";Using import maps (recommended):
json
// deno.json
{
"imports": {
"loglayer": "npm:loglayer@latest"
}
}typescript
// 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:
sh
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:
typescript
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')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.
