npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@cloud-copilot/log

v0.1.22

Published

A lightweight JSON logger

Readme

Log

NPM Version MIT GuardDog Known Vulnerabilities

A lightweight logger to output JSON structured logs for Typescript.

Installation

npm install @cloud-copilot/log

Basic Usage

import { StandardLogger } from '@cloud-copilot/log'

// Create a logger with default log level (warn)
const logger = new StandardLogger()

// Or specify an initial log level
const logger = new StandardLogger('info')

// Log messages at different levels
logger.error('Something went wrong')
logger.warn('This is a warning')
logger.info('Information message')
logger.debug('Debug information')
logger.trace('Trace information')

Log Levels

The logger supports five log levels in order of priority:

  • error (0) - Highest priority
  • warn (1)
  • info (2)
  • debug (3)
  • trace (4) - Lowest priority

Only messages at or above the current log level will be output. For example, if the log level is set to info, then error, warn, and info messages will be logged, but debug and trace will be filtered out.

const logger = new StandardLogger('info')

logger.error('This will be logged') // ✓
logger.warn('This will be logged') // ✓
logger.info('This will be logged') // ✓
logger.debug('This will be filtered') // ✗
logger.trace('This will be filtered') // ✗

Changing Log Level

const logger = new StandardLogger('error')

// Change the log level at runtime
logger.setLogLevel('debug')

// Invalid log levels throw an error
logger.setLogLevel('invalid') // throws Error: Invalid log level: invalid

Structured Logging

The logger automatically creates structured JSON output with timestamps:

const logger = new StandardLogger('info')

logger.info('User logged in')
// Output: {"timestamp":"2023-10-01T12:00:00.000Z","level":"info","message":"User logged in"}

Object Merging

Objects passed as arguments are merged into the log entry:

logger.info('User action', {
  userId: 123,
  action: 'login',
  ip: '192.168.1.1'
})
// Output: {"timestamp":"2023-10-01T12:00:00.000Z","level":"info","message":"User action","userId":123,"action":"login","ip":"192.168.1.1"}

Error Handling

Error objects are specially handled and added to an errors array:

const error = new Error('Database connection failed')

logger.error('Operation failed', error, { userId: 123 })
// Output: {
//   "timestamp": "2023-10-01T12:00:00.000Z",
//   "level": "error",
//   "message": "Operation failed",
//   "userId": 123,
//   "errors": [{
//     "name": "Error",
//     "message": "Database connection failed",
//     "stack": "Error: Database connection failed\n    at ..."
//   }]
// }

Mixed Arguments

The logger handles mixed argument types intelligently:

logger.warn(
  'Processing user', // string message
  { userId: 123 }, // object (merged)
  'with status', // string message
  { status: 'active' }, // object (merged)
  new Error('Minor issue') // error (in errors array)
)
// Output: {
//   "timestamp": "2023-10-01T12:00:00.000Z",
//   "level": "warn",
//   "message": "Processing user with status",
//   "userId": 123,
//   "status": "active",
//   "errors": [{"name": "Error", "message": "Minor issue", "stack": "..."}]
// }

Advanced Examples

Application Logging

import { StandardLogger } from '@cloud-copilot/log'

class UserService {
  private logger = new StandardLogger('info')

  async createUser(userData: any) {
    this.logger.info('Creating user', {
      operation: 'createUser',
      email: userData.email
    })

    try {
      // ... user creation logic
      this.logger.info('User created successfully', {
        userId: newUser.id,
        email: newUser.email
      })
    } catch (error) {
      this.logger.error('Failed to create user', error, {
        email: userData.email
      })
      throw error
    }
  }
}

Environment-based Log Levels

const logLevel = process.env.LOG_LEVEL || 'warn'
const logger = new StandardLogger(logLevel as LogLevel)

// In production: LOG_LEVEL=error (only errors)
// In development: LOG_LEVEL=debug (detailed logging)

Validating Log Levels

Use the isLogLevel utility function to validate log level strings:

import { isLogLevel } from '@cloud-copilot/log'

// Validate user input
const userInput = 'debug'
if (isLogLevel(userInput)) {
  const logger = new StandardLogger(userInput)
} else {
  console.error('Invalid log level provided')
}

// Safe environment variable parsing
const envLogLevel = process.env.LOG_LEVEL
const logLevel = isLogLevel(envLogLevel) ? envLogLevel : 'warn'
const logger = new StandardLogger(logLevel)

// Type guard in functions
function createLoggerFromConfig(config: { logLevel?: string }) {
  if (config.logLevel && isLogLevel(config.logLevel)) {
    return new StandardLogger(config.logLevel)
  }
  return new StandardLogger() // defaults to 'warn'
}

TypeScript Support

Full TypeScript support with proper type definitions:

import { StandardLogger, LogLevel, LogLevels, isLogLevel } from '@cloud-copilot/log'

const logger: StandardLogger = new StandardLogger()
const level: LogLevel = 'info'
const isValid: boolean = isLogLevel('debug') // true