Skip to content

LyricalString/Lovely-Logs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

53 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌈 Lovely Logs

A modern, type-safe logging library that automatically adapts to your environment - whether you're in Node.js, the browser, or AWS Lambda. With beautiful colors, timestamps, and smart platform detection.

✨ Features

  • 🎨 Beautiful, colorful logs with customizable styles
  • πŸ”„ Automatic platform detection (Browser, Node.js, AWS Lambda)
  • ⚑ Zero dependencies
  • πŸ“ Full TypeScript support
  • ⏱️ Built-in timestamps
  • 🎯 Smart object formatting
  • πŸ”’ Type-safe API

πŸ“¦ Installation

# Using npm
npm install lovely-logs

# Using yarn
yarn add lovely-logs

# Using pnpm
pnpm add lovely-logs

# Using bun
bun add lovely-logs

πŸš€ Quick Start

import { logger } from "lovely-logs";

// Start logging right away - platform is auto-detected!
logger.debug("Debug information for development");
logger.info("Hello World!");
logger.warn("This is a warning");
logger.error("This is an error");
logger.success("Operation completed!");

// Log objects with automatic formatting
logger.info({ user: "john", action: "login", timestamp: new Date() });

// Multiple arguments work too
logger.info("User", { id: 123 }, "logged in", true);

// Group related logs
logger.group("User Authentication");
logger.debug("Attempting login...");
logger.success("Login successful");
logger.groupEnd();

🎨 Custom Configuration

import { createLogger } from "lovely-logs";

// Create a custom logger instance
const logger = createLogger({
  // Optional: Override platform auto-detection
  platform: "web", // "web" | "console" | "lambda"
  
  // Optional: Enable/disable timestamps (enabled by default)
  timestampEnabled: true,
  
  // Optional: Custom styles
  customStyles: {
    web: {
      debug: "color: #787878;",
      info: "background: #4CAF50; color: white; padding: 2px 5px; border-radius: 3px;",
      // Customize other levels: warn, error, success
    }
  },
  
  // Optional: Add prefixes to your logs
  prefix: {
    debug: "[DEBUG]",
    info: "[INFO]",
    warn: "[WARN]",
    error: "[ERROR]",
    success: "[SUCCESS]"
  }
});

🎯 Platform-Specific Features

🌐 Browser

  • Beautiful CSS-styled logs
  • Collapsible object formatting
  • Chrome DevTools friendly
  • Debug level with gray styling for development logs

πŸ’» Console (Node.js)

  • Colorful terminal output with Unicode symbols
  • Clean timestamp formatting
  • Perfect for CLI applications
  • Debug level with dimmed output

☁️ AWS Lambda

  • Lambda-optimized output format
  • Automatic detection in Lambda environment
  • Structured logging friendly
  • Standard log levels (DEBUG, INFO, WARN, ERROR)
  • Timestamp prefixing for better log aggregation
  • JSON-formatted object logging for CloudWatch integration
  • Proper log level prefixing ([DEBUG], [INFO], etc.)

πŸ” Advanced Usage

Log Levels

The library supports the following log levels:

  • debug: For development and debugging information
  • info: For general information
  • warn: For warnings
  • error: For errors
  • success: For successful operations
  • group: For grouping related logs
  • groupCollapsed: For collapsible groups (in supported platforms)

You can set the minimum log level in three ways:

  1. Using environment variable:
# Set minimum log level via environment variable
export LOG_LEVEL=INFO  # Will show INFO and above (INFO, WARN, ERROR, SUCCESS)
export LOG_LEVEL=ERROR # Will show ERROR and SUCCESS only
  1. Using options when creating the logger:
const logger = createLogger({
  minLogLevel: "info" // Will show INFO and above
});
  1. Dynamically at runtime:
logger.setMinLogLevel("warn"); // Will show WARN and above

Note: If both environment variable and options are provided, the options take precedence.

Custom Styling

const logger = createLogger({
  customStyles: {
    web: {
      debug: "color: #787878;",
      info: "background: #your-color; color: #text-color;",
      warn: "background: #warn-color; color: #text-color;",
      error: "background: #error-color; color: #text-color;",
      success: "background: #success-color; color: #text-color;",
    },
    console: {
      // Custom terminal colors and symbols
      debug: "\x1b[90mβ—―\x1b[0m",
      info: "\x1b[36m●\x1b[0m",
      // ... customize other levels
    },
    lambda: {
      // Custom Lambda log prefixes
      debug: "[DEBUG]",
      info: "[INFO]",
      // ... customize other levels
    }
  }
});

AWS Lambda Integration

When running in an AWS Lambda environment, the logger automatically:

  1. Detects the Lambda environment
  2. Formats logs appropriately for CloudWatch
  3. Adds proper log level prefixes
  4. Maintains timestamp information
  5. Formats objects as JSON strings

Example Lambda usage:

import { createLogger } from "lovely-logs";

export const handler = async (event: any) => {
  const logger = createLogger(); // Will auto-detect Lambda environment
  
  logger.debug("Event received:", event);
  logger.info("Processing started");
  
  try {
    // Your Lambda logic here
    logger.success("Processing completed");
  } catch (error) {
    logger.error("Processing failed:", error);
    throw error;
  }
};

Singleton Pattern

The logger uses a singleton pattern, ensuring consistent logging across your application:

// These both reference the same logger instance
import { logger } from "lovely-logs";
import { createLogger } from "lovely-logs";

const customLogger = createLogger(); // Returns existing instance if already created

Reset for Testing

import { Logger } from "lovely-logs";

// Reset the logger (useful in tests)
Logger.resetInstance();

πŸ“„ License

MIT License - feel free to use in your projects!

🀝 Contributing

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

Publishing to NPM

To publish a new version to NPM:

  1. Make sure you're logged in to npm:

    npm login
  2. Ensure all your changes are committed:

    git add .
    git commit -m "feat: your changes description"
  3. Run npm publish:

    npm publish

    This will automatically:

    • Run the prepare script
    • Build the package
    • Publish to NPM
    • You might be asked for a one-time password (OTP) if you have 2FA enabled

πŸ’¬ Support

If you encounter any issues or have questions, please open an issue on our GitHub repository.

Happy logging! 🌈

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •