@very-coffee/hello
v1.1.2
Published
A type-safe utility for creating structured logging across multiple namespaces and environments in TypeScript applications
Downloads
24
Readme
@very-coffee/hello
A type-safe utility for creating structured logging across multiple namespaces and environments in TypeScript applications.
Features
- Type-safe logging with namespace and environment level support
- DEBUG environment variable support (similar to debug package)
- Pretty printing in development
- File logging support
- Next.js compatibility mode
- Configurable log levels
- Object and formatted string logging
Installation
npm install @very-coffee/helloBasic Usage
import { helloInnit } from "@very-coffee/hello";
const namespaces = ["app", "api"] as const;
const levels = ["info", "error", "debug"] as const;
const hello = helloInnit(namespaces, levels);
// Log messages
hello.app.info("Server started on port %d", 3000);
hello.api.error({ error: new Error("Failed") }, "Request failed");Configuration
Basic Options
const hello = helloInnit(namespaces, levels, {
prettyPrint: true, // Enable pretty printing (default: true in development)
level: "debug", // Set minimum log level
});File Logging
const hello = helloInnit(namespaces, levels, {
file: {
path: "./logs/app.log", // Path to log file
level: "info", // Optional: minimum level for file logging
prettyPrint: true, // Optional: enable pretty printing in file
},
});Next.js Compatibility
For Next.js applications, especially in edge runtime environments where worker threads aren't supported:
// In your logger configuration file (e.g., src/lib/logs.ts)
import { helloInnit } from "@very-coffee/hello";
const namespaces = ["app", "api"] as const;
const levels = ["info", "error", "debug"] as const;
export const hello = helloInnit(namespaces, levels, {
disableWorkers: true, // Required for Next.js edge runtime
prettyPrint: process.env.NODE_ENV === "development",
});When using in Next.js:
- Always set
disableWorkers: trueto avoid worker thread errors - The logger will automatically use synchronous logging in edge runtime
- File logging will use direct file streams instead of worker threads
- Pretty printing will work in development mode without workers
Combined Configuration
You can combine different options:
const hello = helloInnit(namespaces, levels, {
disableWorkers: true,
prettyPrint: true,
file: {
path: "./logs/app.log",
level: "info",
prettyPrint: true,
},
});Environment Variables
DEBUG
Control which namespaces and levels are enabled:
# Enable specific namespace and level
DEBUG=app:info
# Enable all levels for a namespace
DEBUG=app:*
# Enable specific level across all namespaces
DEBUG=*:error
# Enable everything
DEBUG=*
# Enable multiple patterns
DEBUG=app:info,api:error
# Disable specific namespaces
DEBUG=*,-apiLOG_LEVEL
Set the minimum log level:
LOG_LEVEL=debug # Will show debug and above
LOG_LEVEL=info # Will show info and above
LOG_LEVEL=error # Will show only errorNODE_ENV
Controls default pretty printing:
development: Pretty printing enabled by defaultproduction: Pretty printing disabled by defaulttest: Transport disabled by default
Advanced Usage
Custom Pino Configuration
You can pass any Pino options directly:
import { helloInnit, pino } from "@very-coffee/hello";
const hello = helloInnit(namespaces, levels, {
// Any valid pino options
timestamp: pino.stdTimeFunctions.isoTime,
});Object Logging
// Log with context object
hello.app.info({ userId: "123" }, "User logged in");
// Log object directly
hello.app.debug({
event: "cache_miss",
key: "user_123",
});Format Specifiers
Supports printf-style format specifiers:
%s- String%dor%i- Integer%f- Float%oor%O- Object%j- JSON
Example:
hello.app.info("User %s logged in from %s", username, ip);License
MIT
