@fiftyten/logger
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

@fiftyten/logger

A lightweight and flexible logging utility for TypeScript/JavaScript applications with environment-aware features.

Features

  • Namespace-based logging
  • Log level support (debug, info, warn, error)
  • Production environment detection
  • Environment-aware logging control
  • TypeScript support
  • Zero dependencies
  • Tree-shakeable

Installation

npm install @fiftyten/logger
# or
yarn add @fiftyten/logger
# or
pnpm add @fiftyten/logger

Usage

Basic Usage

import { createLogger } from '@fiftyten/logger'

// Create logger with namespace
const logger = createLogger('MyApp')

logger.debug('Debug message')
logger.info('Info message')
logger.warn('Warning message')
logger.error('Error message')

// Multiple arguments support
logger.info('User logged in:', { userId: 123, time: new Date() })

Advanced Configuration

const logger = createLogger({
	namespace: 'MyApp',
	level: 'warn', // Only outputs warn and error levels
	enabled: true, // Enable/disable logging
	isProd: () => process.env.NODE_ENV === 'production', // Custom production check
})

// Dynamically change log level
logger.setLevel('error') // Now only errors will be logged

// Temporarily disable logging
logger.disable()

// Re-enable logging
logger.enable()

Environment Behavior

Development

  • All log levels are displayed
  • Detailed logging with namespace information
  • Full debugging capabilities

Production

  • Only error level is logged by default
  • Configurable through isProd option
  • Prevents unnecessary logging in production

API Reference

createLogger

function createLogger(namespace: string): Logger
function createLogger(config: LoggerConfig): Logger

interface LoggerConfig {
	namespace: string
	level?: 'debug' | 'info' | 'warn' | 'error'
	enabled?: boolean
	isProd?: () => boolean
}

interface Logger {
	debug: (...args: unknown[]) => void
	info: (...args: unknown[]) => void
	warn: (...args: unknown[]) => void
	error: (...args: unknown[]) => void
	setLevel: (level: LogLevel) => void
	enable: () => void
	disable: () => void
}

Environment Detection

By default, the logger checks the following environment variables to detect production mode:

  • process.env.NODE_ENV
  • import.meta.env.MODE
  • import.meta.env.PROD
  • import.meta.env.VITE_APP_ENV

For custom environment detection, you can provide your own implementation through the isProd option.

Examples

React Component

function UserProfile() {
  const logger = createLogger('UserProfile')

  useEffect(() => {
    logger.debug('Component mounted')
    return () => logger.debug('Component unmounted')
  }, [])

  logger.info('Rendering profile...')
  return <div>User Profile</div>
}

API Service

class ApiService {
	private logger = createLogger('ApiService')

	async fetchData() {
		try {
			this.logger.info('Fetching data...')
			// ... fetch logic
		} catch (error) {
			this.logger.error('Failed to fetch data:', error)
			throw error
		}
	}
}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT

Dependents (0)

Package Sidebar

Install

npm i @fiftyten/logger

Weekly Downloads

5

Version

0.1.2

License

MIT

Unpacked Size

13 kB

Total Files

8

Last publish

Collaborators

  • weirdry
  • oliver0214