DEV Community

Theo Gravity
Theo Gravity

Posted on

Why LogLayer is the Logging Framework for Deno

Deno developers face a logging dilemma. Console logging is simple but limited. Traditional Node.js libraries like Winston or Pino aren't built for Deno's environment. LogLayer solves this by providing a unified logging layer designed for Deno, offering flexibility to adapt your logging infrastructure as your application grows without changing your code.

What Makes LogLayer Different

The framework provides a consistent, fluent API for specifying log messages, metadata, and errors, regardless of which underlying transport you're using. This means you can start with simple console logging during development, then seamlessly transition to more sophisticated logging solutions as your application grows, all without changing your logging code.

Here's a quick example of LogLayer's expressive API in action:

log.withContext({ userId: 123, environment: "production" })
  .withError(new Error("Connection failed"))
  .withMetadata({ retryCount: 3 })
  .error("Failed to connect to database")
Enter fullscreen mode Exit fullscreen mode

All the context, error details, and metadata are captured in one fluent chain, making it easy to attach structured data to your log entries without cluttering your code.

Installation and Setup

Getting started with LogLayer in a Deno project is straightforward. You have two primary options for installation. The first approach uses npm specifiers directly in your import statements:

import { LogLayer, ConsoleTransport } from "npm:loglayer@latest";
Enter fullscreen mode Exit fullscreen mode

For better dependency management in production, LogLayer works with Deno's import map system. Create a deno.json file:

{
  "imports": {
    "loglayer": "npm:loglayer@latest"
  }
}
Enter fullscreen mode Exit fullscreen mode

Then import using clean module paths:

import { LogLayer, ConsoleTransport } from "loglayer";
Enter fullscreen mode Exit fullscreen mode

You can also extend LogLayer with Deno-compatible logging libraries like LogTape and tslog. To use tslog, for example:

import { Logger } from "npm:tslog@latest";
import { LogLayer } from "npm:loglayer@latest";
import { TsLogTransport } from "npm:@loglayer/transport-tslog@latest";

const tslog = new Logger();

const log = new LogLayer({
  transport: new TsLogTransport({
    logger: tslog
  })
});

log.withMetadata({ userId: "123" }).info("Processing user request");
Enter fullscreen mode Exit fullscreen mode

All your logging code remains unchanged when swapping transports, making it easy to experiment with different backends without rewriting your application code.

Basic Example Usage

The simplest way to begin using LogLayer in Deno is with the built-in Console Transport. This requires no additional packages and works immediately in any Deno environment.

import { LogLayer, ConsoleTransport } from "npm:loglayer@latest";

const log = new LogLayer({
  transport: new ConsoleTransport({
    logger: console
  })
});

log.info("Hello from Deno with LogLayer!");
Enter fullscreen mode Exit fullscreen mode

While console logging is useful for development, the real power of LogLayer becomes evident when you need more sophisticated logging capabilities. The framework's fluent API makes it easy to attach metadata and errors to log entries.

log.withContext({
  userId: 12345,
  environment: "production"
});

log.withMetadata({
  action: "user_login",
  ipAddress: "192.168.1.1"
}).info("User logged in successfully");

log.withError(new Error("Database connection failed"))
  .withMetadata({ query: "SELECT * FROM users" })
  .error("Query execution failed");
Enter fullscreen mode Exit fullscreen mode

The context data persists across all subsequent log calls, automatically including the user ID and environment in every log entry. Meanwhile, metadata can be added per log call to provide specific details about that particular event. Errors are handled consistently, with LogLayer serializing error objects in a standardized format that works across all transports.

Pretty Terminal Logging

For development, LogLayer supports the Simple Pretty Terminal transport, which provides beautifully formatted, color-coded log output in your Deno terminal:

import { LogLayer } from "npm:loglayer@latest";
import { getSimplePrettyTerminal } from "npm:@loglayer/transport-simple-pretty-terminal@latest";

const log = new LogLayer({
  transport: getSimplePrettyTerminal({
    runtime: "node", // Use "node" for Deno
    viewMode: "inline", // "inline" | "message-only" | "expanded"
  })
});

log.withMetadata({ userId: 123, action: "login" }).info("User logged in");
Enter fullscreen mode Exit fullscreen mode

This transport offers multiple view modes and customizable themes, making it easier to read and debug logs during development. The transport works seamlessly with Deno and doesn't require any Node-specific dependencies.

Conclusion

LogLayer provides Deno developers with a production-ready logging solution that scales from simple console output to sophisticated backends like LogTape and tslog, all without changing your code. The framework's transport-based architecture and fluent API make it an ideal choice for any Deno project, whether you're building a CLI tool or a production application.

Top comments (0)