@maxjan/logger
v0.1.5
Published
Development logger that sends console output to Loki
Maintainers
Readme
@maxjan/logger
Development logger that wraps console and sends all logs to Loki/Grafana. Works with any JavaScript/TypeScript project.
In production, console works as normal - no logs are sent anywhere.
Installation
npm install @maxjan/loggerQuick Start
// lib/logger.ts
import { initLogger } from "@maxjan/logger";
initLogger({
appName: "my-app",
});// app/layout.tsx or index.ts - import once at app startup
import "./lib/logger";
// Now all console calls are sent to Loki
console.log("Hello");
console.warn("Warning!");
console.error("Something failed", { userId: 123 });Configuration
initLogger({
// Required
appName: "my-app",
// Optional
lokiUrl: "http://127.0.0.1:3100/loki/api/v1/push", // default
enabled: true, // default, set false to disable
locale: "sv-SE", // default, for timestamp formatting
labels: {
env: "development",
version: "1.0.0",
},
});API
initLogger(config)
Initialize the logger. Call once at app startup.
import { initLogger } from "@maxjan/logger";
initLogger({
appName: "my-app",
labels: { service: "api" },
});setLabels(labels)
Add or update labels at runtime. Useful for adding request-specific context.
import { setLabels } from "@maxjan/logger";
setLabels({ userId: "123", requestId: "abc" });disableLogger()
Disable the logger and restore original console behavior.
import { disableLogger } from "@maxjan/logger";
disableLogger();Supported Console Methods
| Method | Loki Level |
| ---------------- | ---------- |
| console.log() | info |
| console.info() | info |
| console.warn() | warn |
| console.error() | error |
| console.debug() | debug |
| console.trace() | debug |
Example: Next.js
// lib/logger.ts
import { initLogger } from "@maxjan/logger";
initLogger({
appName: "my-nextjs-app",
labels: {
framework: "nextjs",
},
});// app/layout.tsx
import "@/lib/logger";
export default function RootLayout({ children }) {
return <html>{children}</html>;
}Example: Express
// index.ts
import { initLogger, setLabels } from "@maxjan/logger";
import express from "express";
initLogger({ appName: "my-api" });
const app = express();
app.use((req, res, next) => {
setLabels({
path: req.path,
method: req.method,
});
next();
});
app.get("/", (req, res) => {
console.log("Request received");
res.send("Hello");
});Querying in Grafana
# All logs from app
{app="my-app"}
# Only errors
{app="my-app", level="error"}
# Parse JSON and filter
{app="my-app"} | json | message =~ "user.*"Requirements
- Loki running locally (default:
http://127.0.0.1:3100) - Node.js 18+
Development
npm install
npm run build
npm run dev # watch mode
npm run test # run tests
npm run lint # check linting
npm run format # format with prettierCommit Format
This project uses Conventional Commits:
feat: add new feature # minor version bump
fix: bug fix # patch version bump
feat!: breaking change # major version bump
docs: update documentation
chore: maintenance
refactor: code refactoring
perf: performance improvementRelease
npm run release # auto-bump based on commits
npm run release:minor # force minor bump
npm run release:major # force major bumpThis will:
- Bump version in package.json
- Generate/update CHANGELOG.md
- Create git tag
License
MIT
