{"_id":"@1984vc/otel-http-logger","name":"@1984vc/otel-http-logger","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@1984vc/otel-http-logger","version":"0.0.1","description":"A lightweight OpenTelemetry logger for Node.js, Bun, and browsers with zero dependencies","main":"dist/index.js","module":"dist/index.mjs","types":"dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.js"}},"scripts":{"build":"tsup src/index.ts --format cjs,esm --dts","test":"vitest run","test:watch":"vitest","test:coverage":"vitest run --coverage","lint":"tsc --noEmit","clean":"rm -rf dist","prepublishOnly":"npm run clean && npm run build && npm run test"},"keywords":["opentelemetry","otel","logging","logger","otlp","tracing","observability","bun"],"author":"","license":"MIT","devDependencies":{"@types/node":"^20.19.1","tsup":"^8.5.0","typescript":"^5.8.3","vitest":"^1.6.1"},"engines":{"node":">=18.0.0","bun":">=1.0.0"},"_id":"@1984vc/otel-http-logger@0.0.1","gitHead":"727d85c039a5555f846c1f6ed657f00b2927e0dd","_nodeVersion":"20.19.2","_npmVersion":"10.8.2","dist":{"integrity":"sha512-9ZUdRoWZ8TPqFdcof/nIFdlgPwj/Yf3L/hmd8G/CFwuLahG5hRFlfxz20gnqvnZQbJ2ws/7eH/ITabyrKNgPXw==","shasum":"85d016566581099859f9ac1fc4c5796fb8efe63f","tarball":"https://registry.npmjs.org/@1984vc/otel-http-logger/-/otel-http-logger-0.0.1.tgz","fileCount":7,"unpackedSize":42848,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIDysU+ZBl8sleJPWm1xGTdF18aPuL4MXcInX4TGUG7OxAiEA/dHwfrxdp2R+oB9GnLclAqUWjTNMhKVemDLLrjuNwwI="}]},"_npmUser":{"name":"mdp","email":"m@mdp.im","actor":{"name":"mdp","email":"m@mdp.im","type":"user"}},"directories":{},"maintainers":[{"name":"mdp","email":"m@mdp.im"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/otel-http-logger_0.0.1_1750682817082_0.08516951646243909"},"_hasShrinkwrap":false}},"time":{"created":"2025-06-23T12:46:56.995Z","0.0.1":"2025-06-23T12:46:57.252Z","modified":"2025-06-23T12:46:57.527Z"},"maintainers":[{"name":"mdp","email":"m@mdp.im"}],"description":"A lightweight OpenTelemetry logger for Node.js, Bun, and browsers with zero dependencies","keywords":["opentelemetry","otel","logging","logger","otlp","tracing","observability","bun"],"license":"MIT","readme":"# otel-http-logger\n\nA lightweight OpenTelemetry logger for Node.js, Bun, and browsers with zero dependencies and support for async context propagation. Includes built-in support for HyperDX.\n\n## Features\n\n- **Zero Dependencies**: Works with vanilla JavaScript/Node.js/Bun\n- **OpenTelemetry Compatible**: Sends logs to any OTLP HTTP collector\n- **Context Propagation**: Uses AsyncLocalStorage for automatic logger context across async boundaries\n- **TypeScript Support**: Written in TypeScript with full type definitions\n- **Compatible with many JS runtimes**: NodeJS, Browser, Bun, Cloudflare(Miniflare), Deno\n\n## Installation\n\n```bash\nnpm install @1984vc/otel-http-logger\n```\n\nOr with yarn:\n\n```bash\nyarn add @1984vc/otel-http-logger\n```\n\nOr with bun:\n\n```bash\nbun add @1984vc/otel-http-logger\n```\n\n## Quick Start\n\n```javascript\nconst { initializeLogger, LogLevel } = require('otel-http-logger');\n\n// Initialize a console-only logger (no OTLP)\nconst logger = initializeLogger({\n  endpoint: '', // Empty endpoint means no OTLP logging\n  headers: {},\n  serviceName: 'my-service',\n  environment: 'development'\n});\n\n// Log at different levels\nlogger.debug('Debug message');\nlogger.info('Info message');\nlogger.warn('Warning message');\nlogger.error('Error message');\n\n// Log with additional attributes\nlogger.info('User logged in', {\n  userId: '12345',\n  loginTime: new Date().toISOString()\n});\n\n// Log errors with stack traces\ntry {\n  throw new Error('Something went wrong');\n} catch (error) {\n  logger.error('An error occurred', error, {\n    component: 'authentication'\n  });\n}\n```\n\n## OTLP Logging\n\n### HyperDX Example\n\n```javascript\nconst { initializeLogger } = require('otel-http-logger');\n\n// Get HyperDX API key from environment variable\nconst HYPERDX_API_KEY = process.env.HYPERDX_API_KEY;\n\n// Initialize a logger with HyperDX configuration\nconst logger = initializeLogger({\n  endpoint: 'https://in-otel.hyperdx.io/v1/logs',\n  headers: {\n    'Authorization': HYPERDX_API_KEY,\n    'Content-Type': 'application/json'\n  },\n  serviceName: 'my-service',\n  environment: 'production'\n});\n\n// Log as usual\nlogger.info('This will be sent to HyperDX');\n\n// Important: Flush logs before your application exits\nawait logger.flush();\n```\n\n## Context Propagation\n\nThe logger supports context propagation using AsyncLocalStorage, which allows you to create contextual loggers that are automatically available throughout your async call stack:\n\n```javascript\nconst { initializeLogger, createLogger } = require('otel-http-logger');\n\nconst logger = initializeLogger({\n  endpoint: 'https://your-otlp-collector/v1/logs',\n  headers: { 'Authorization': 'Bearer your-token' },\n  serviceName: 'my-service',\n  environment: 'production'\n});\n\n// Use withLogger to set the current logger in the async context\nawait logger.withLogger(async () => {\n  // This function and all async functions called from it\n  // will have access to the logger via createLogger\n  \n  await processRequest();\n});\n\nasync function processRequest() {\n  // Create a contextual logger using the current logger from async context\n  const requestLogger = createLogger('request');\n  \n  requestLogger.info('Processing request');\n  \n  // Even in nested async functions, the logger is available\n  await callDatabase();\n}\n\nasync function callDatabase() {\n  const dbLogger = createLogger('database');\n  dbLogger.info('Executing query');\n}\n```\n\n## API Reference\n\n### `initializeLogger(config)`\n\nInitializes a new logger with the provided configuration.\n\n```typescript\ninterface LoggerConfig {\n  endpoint: string;        // OTLP HTTP collector endpoint URL\n  headers: Record<string, string>; // Headers for OTLP HTTP collector\n  serviceName: string;     // Service name for OTLP resource\n  environment: string;     // Environment name (e.g., 'production', 'staging')\n}\n```\n\n### `createLogger(context)`\n\nCreates a contextual logger using the current logger from the async context.\n\n```typescript\nfunction createLogger(context: string): ContextLogger;\n```\n\n### `getCurrentLogger()`\n\nGets the current logger from the async context.\n\n```typescript\nfunction getCurrentLogger(): Logger;\n```\n\n### `Logger`\n\nThe main logger class.\n\n```typescript\nclass Logger implements ContextLogger {\n  constructor(config?: LoggerConfig, contextPrefix?: string);\n  \n  debug(message: string, attributes?: Record<string, any>): void;\n  info(message: string, attributes?: Record<string, any>): void;\n  warn(message: string, attributes?: Record<string, any>): void;\n  error(message: string, error?: Error, attributes?: Record<string, any>): void;\n  \n  newContext(context: string): ContextLogger;\n  \n  async withLogger<T>(fn: () => T | Promise<T>): Promise<T>;\n  \n  async flush(): Promise<void>;\n}\n```\n\n### `LogLevel`\n\nEnum for log levels.\n\n```typescript\nenum LogLevel {\n  DEBUG = 'DEBUG',\n  INFO = 'INFO',\n  WARN = 'WARN',\n  ERROR = 'ERROR'\n}\n```\n\n## Examples\n\nSee the [examples](./examples) directory for more detailed examples:\n\n- [Basic Usage](./examples/basic.js) - Demonstrates basic logging and HyperDX integration\n- [Context Propagation](./examples/context.js) - Shows how to use async context for logging\n\nTo run the examples with [HyperDX](https://hyperdx.io) integration:\n\n```bash\n# Set your HyperDX API key\nexport HYPERDX_API_KEY=your_api_key_here\n\n# Run the examples with Node.js\nnode examples/basic.js\nnode examples/context.js\n\n# Or run with Bun\nbun examples/basic.js\nbun examples/context.js\n```\n\n## License\n\nMIT\n","readmeFilename":"README.md","_rev":"1-c266c474f8cdd48789b1caf914a89c80"}