11# @deepracticex/logger
22
3- Unified logging system for Deepractice projects using [ Pino ] ( https://github.com/pinojs/pino ) .
3+ Universal logging system for all JavaScript runtimes - Node.js, Cloudflare Workers, Browser, and more .
44
55## Features
66
7- - 🎨 Console output with pretty print
8- - 📁 File logging with daily rotation
9- - 📍 Automatic caller location tracking
10- - 🎯 Configurable log levels
11- - 🌈 Color support (respects MCP stdio mode)
12- - ⚡ Electron compatibility (sync mode)
13- - 🔧 TypeScript support
7+ - 🌍 ** Universal** - Works in Node.js, Cloudflare Workers, Browser, and other JavaScript runtimes
8+ - 🎯 ** Platform-Specific Optimizations** - Pino for Node.js, lightweight console for edge/browser
9+ - 📦 ** Tree-Shakeable** - Only bundles the code you need for your platform
10+ - 🎨 ** Pretty Console Output** - Color support with automatic MCP stdio detection
11+ - 📁 ** File Logging** - Daily rotation for Node.js (when enabled)
12+ - 📍 ** Caller Location Tracking** - Automatic file/line tracking
13+ - 🔧 ** TypeScript Support** - Full type safety
14+ - ⚡ ** Zero Config** - Sensible defaults, customizable when needed
1415
1516## Installation
1617
1718``` bash
1819pnpm add @deepracticex/logger
1920```
2021
21- ## Usage
22+ ## Platform-Specific Entry Points
2223
23- ### Basic Usage
24+ Choose the right entry point for your platform:
25+
26+ ### Node.js (Default)
27+
28+ ``` typescript
29+ // Uses Pino for high performance logging
30+ import { createLogger } from " @deepracticex/logger" ;
31+ // or explicitly
32+ import { createLogger } from " @deepracticex/logger/nodejs" ;
33+
34+ const logger = createLogger ({
35+ level: " info" ,
36+ name: " my-service" ,
37+ console: true ,
38+ file: true , // File logging with daily rotation
39+ });
40+
41+ logger .info (" Server started" );
42+ ```
43+
44+ ### Cloudflare Workers
45+
46+ ``` typescript
47+ // Uses lightweight console adapter (no Node.js dependencies)
48+ import { createLogger } from " @deepracticex/logger/cloudflare-workers" ;
49+
50+ const logger = createLogger ({
51+ level: " info" ,
52+ name: " my-worker" ,
53+ console: true ,
54+ });
55+
56+ logger .info (" Request handled" );
57+ ```
58+
59+ ### Browser
60+
61+ ``` typescript
62+ // Uses browser-optimized console adapter
63+ import { createLogger } from " @deepracticex/logger/browser" ;
64+
65+ const logger = createLogger ({
66+ level: " debug" ,
67+ name: " my-app" ,
68+ console: true ,
69+ colors: true ,
70+ });
71+
72+ logger .info (" App initialized" );
73+ ```
74+
75+ ## Quick Start
76+
77+ ### Default Logger (Node.js)
2478
2579``` typescript
2680import { info , warn , error , debug } from " @deepracticex/logger" ;
@@ -31,14 +85,14 @@ error("Connection failed");
3185debug (" Debug info" );
3286```
3387
34- ### Custom Logger Instance
88+ ### Custom Logger
3589
3690``` typescript
3791import { createLogger } from " @deepracticex/logger" ;
3892
3993const logger = createLogger ({
4094 level: " debug" ,
41- name: " @deepracticex/account -service" ,
95+ name: " @deepracticex/my -service" ,
4296 console: true ,
4397 file: {
4498 dirname: " /var/log/myapp" ,
@@ -55,15 +109,24 @@ logger.info("Custom logger initialized");
55109
56110``` typescript
57111interface LoggerConfig {
58- level? : string ; // 'fatal' | 'error' | 'warn' | 'info' | 'debug' | 'trace'
59- console? : boolean ; // Enable console output (default: true)
112+ // Log level (default: 'info')
113+ level? : " fatal" | " error" | " warn" | " info" | " debug" | " trace" ;
114+
115+ // Package/service name (default: 'app')
116+ name? : string ;
117+
118+ // Console output (default: true)
119+ console? : boolean ;
120+
121+ // File logging - Node.js only (default: false)
60122 file? :
61123 | boolean
62124 | {
63125 dirname? : string ; // Log directory (default: ~/.deepractice/logs)
64126 };
65- colors? : boolean ; // Enable colors (default: true, auto-disabled in MCP stdio)
66- name? : string ; // Package/service name (default: 'app')
127+
128+ // Color support (default: true, auto-disabled in MCP stdio)
129+ colors? : boolean ;
67130}
68131```
69132
@@ -75,38 +138,177 @@ interface LoggerConfig {
75138
76139## Log Levels
77140
78- - ` fatal ` - Critical errors
79- - ` error ` - Errors
80- - ` warn ` - Warnings
81- - ` info ` - General information
82- - ` debug ` - Debug information
83- - ` trace ` - Verbose trace
141+ - ` fatal ` - Critical errors that require immediate attention
142+ - ` error ` - Errors that need to be fixed
143+ - ` warn ` - Warnings about potential issues
144+ - ` info ` - General information (default)
145+ - ` debug ` - Detailed debug information
146+ - ` trace ` - Very verbose trace information
147+
148+ ## Platform Details
84149
85- ## File Logging
150+ ### Node.js
86151
87- Logs are automatically rotated daily:
152+ Uses [ Pino] ( https://github.com/pinojs/pino ) for high-performance logging:
153+
154+ - File logging with daily rotation
155+ - Automatic caller location tracking
156+ - Worker threads for better performance
157+ - MCP stdio mode detection
158+
159+ ** File Structure:**
88160
89161```
90162~/.deepractice/logs/
91- ├── deepractice-2025-10-08.log # All logs
92- └── deepractice-error-2025-10-08.log # Error logs only
163+ ├── deepractice-2025-10-13.log # All logs
164+ └── deepractice-error-2025-10-13.log # Error logs only
165+ ```
166+
167+ ### Cloudflare Workers
168+
169+ Uses lightweight console adapter:
170+
171+ - Minimal bundle size (~ 1.5KB)
172+ - No Node.js dependencies
173+ - Full logging API compatibility
174+ - Works with Wrangler dev and production
175+
176+ ### Browser
177+
178+ Uses browser-optimized console adapter:
179+
180+ - Native console API
181+ - Color support
182+ - Source map integration
183+ - DevTools friendly
184+
185+ ## Examples
186+
187+ ### Node.js Service
188+
189+ ``` typescript
190+ import { createLogger } from " @deepracticex/logger" ;
191+
192+ const logger = createLogger ({
193+ level: process .env .LOG_LEVEL || " info" ,
194+ name: " @deepracticex/api-server" ,
195+ console: true ,
196+ file: {
197+ dirname: " ./logs" ,
198+ },
199+ });
200+
201+ logger .info ({ port: 3000 }, " Server started" );
202+ logger .error ({ err: error }, " Database connection failed" );
93203```
94204
95- ## Caller Location
205+ ### Cloudflare Worker
206+
207+ ``` typescript
208+ import { createLogger } from " @deepracticex/logger/cloudflare-workers" ;
209+
210+ export default {
211+ async fetch(request : Request , env : Env ): Promise <Response > {
212+ const logger = createLogger ({
213+ name: " my-worker" ,
214+ level: env .LOG_LEVEL || " info" ,
215+ });
216+
217+ logger .info ({ url: request .url }, " Request received" );
218+
219+ try {
220+ // Handle request
221+ return new Response (" OK" );
222+ } catch (error ) {
223+ logger .error ({ error }, " Request failed" );
224+ return new Response (" Error" , { status: 500 });
225+ }
226+ },
227+ };
228+ ```
96229
97- Automatically tracks and displays file location:
230+ ### Browser App
98231
232+ ``` typescript
233+ import { createLogger } from " @deepracticex/logger/browser" ;
234+
235+ const logger = createLogger ({
236+ name: " my-app" ,
237+ level: " debug" ,
238+ colors: true ,
239+ });
240+
241+ logger .info (" App initialized" );
242+
243+ document .addEventListener (" click" , (e ) => {
244+ logger .debug ({ target: e .target }, " User clicked" );
245+ });
99246```
100- [2025-10-08 10:30:00] INFO: @deepracticex/account-service [index.ts:42] Server started
247+
248+ ## Architecture
249+
250+ The logger uses a two-adapter architecture:
251+
252+ 1 . ** Pino Adapter** - For Node.js (high performance, file support)
253+ 2 . ** Console Adapter** - For edge/browser (lightweight, universal)
254+
255+ Platform-specific entry points ensure only the necessary adapter is bundled:
256+
257+ ```
258+ @deepracticex/logger → nodejs.ts → pino-adapter
259+ @deepracticex/logger/nodejs → nodejs.ts → pino-adapter
260+ @deepracticex/logger/cloudflare-workers → cloudflare-workers.ts → console-adapter
261+ @deepracticex/logger/browser → browser.ts → console-adapter
262+ ```
263+
264+ ## Bundle Size
265+
266+ - Node.js: Full pino functionality (~ 200KB with dependencies)
267+ - Cloudflare Workers: ~ 1.5KB (console adapter only)
268+ - Browser: ~ 1.5KB (console adapter only)
269+
270+ ## FAQ
271+
272+ ### Why platform-specific entry points?
273+
274+ This allows bundlers (esbuild, webpack, etc.) to tree-shake unused code. If you use the Cloudflare Workers entry, the 200KB+ pino dependency won't be included in your bundle.
275+
276+ ### Can I use the same logger across different files?
277+
278+ Yes! Create a logger module:
279+
280+ ``` typescript
281+ // src/infrastructure/logger/index.ts
282+ import { createLogger } from " @deepracticex/logger/cloudflare-workers" ;
283+
284+ export const logger = createLogger ({
285+ name: " my-app" ,
286+ level: " info" ,
287+ });
288+ ```
289+
290+ Then import everywhere:
291+
292+ ``` typescript
293+ import { logger } from " ~/infrastructure/logger" ;
294+
295+ logger .info (" Hello from any file!" );
101296```
102297
103- ## MCP Compatibility
298+ ### Does it work with monorepos?
299+
300+ Yes! Each package can use the appropriate entry point:
104301
105- When running in MCP stdio mode (` MCP_TRANSPORT=stdio ` ), colors are automatically disabled to prevent ANSI escape codes from interfering with JSON-RPC messages.
302+ ``` typescript
303+ // apps/api (Node.js)
304+ import { createLogger } from " @deepracticex/logger" ;
106305
107- ## Electron Compatibility
306+ // apps/worker (Cloudflare)
307+ import { createLogger } from " @deepracticex/logger/cloudflare-workers" ;
108308
109- When running in Electron or when ` DEEPRACTICE_NO_WORKERS=true ` is set, the logger uses sync mode to avoid worker thread issues.
309+ // apps/web (Browser)
310+ import { createLogger } from " @deepracticex/logger/browser" ;
311+ ```
110312
111313## License
112314
0 commit comments