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 🙏

© 2024 – Pkg Stats / Ryan Hefner

nexlog

v3.1.0

Published

A simple and effective logging library for Next.js, compatible with server, browser, and edge environments.

Downloads

26

Readme

nexlog

nexlog is a simple, effective, and zero-dependency logging library for Next.js, compatible with server, browser, and edge environments. It's distributed as TypeScript files for maximum flexibility and type safety.

CI/CD npm version License

Features

  • Environment-aware logging (Server, Browser, Edge)
  • Customizable log levels
  • Colored console output for server environments
  • Full TypeScript support
  • Lightweight and easy to use
  • Zero external dependencies
  • No side effects
  • Distributed as TypeScript files for maximum flexibility

Installation

npm install nexlog
# or
yarn add nexlog
# or
bun add nexlog

Configuration with Next.js

To use nexlog with Next.js, follow these steps:

  1. Install nexlog as shown above.

  2. Configure Next.js to transpile nexlog. In your next.config.js or next.config.mjs:

/** @type {import('next').NextConfig} */
const nextConfig = {
  transpilePackages: ["nexlog"],
};

export default nextConfig;
  1. Use the LoggerProvider in your root layout (e.g., in app/layout.tsx):
import { LoggerProvider } from 'nexlog/react';

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <LoggerProvider>
          {children}
        </LoggerProvider>
      </body>
    </html>
  );
}
  1. In your components, use the useLogger hook:
'use client';
import { useLogger } from 'nexlog/react';

export default function MyComponent() {
  const logger = useLogger();
  logger.info('MyComponent rendered');
  return <div>My Component</div>;
}

Usage

import logger from 'nexlog';

// Log messages
logger.info('This is an info message');
logger.warn('This is a warning', { additionalInfo: 'Some extra data' });
logger.error('An error occurred', { errorCode: 500 });

Note: The default export is an instance of the ConfigurableLogger class, pre-configured for the detected environment.

Output Example

Server environment:

[INFO] This is an info message
[WARN] This is a warning {"additionalInfo":"Some extra data"}
[ERROR] An error occurred {"errorCode":500}

Browser/Edge environment:

[INFO] This is an info message
[WARN] This is a warning {"additionalInfo":"Some extra data"}
[ERROR] An error occurred {"errorCode":500}

API

Methods

  • logger.trace(msg: string, meta?: object)
  • logger.debug(msg: string, meta?: object)
  • logger.info(msg: string, meta?: object)
  • logger.warn(msg: string, meta?: object)
  • logger.error(msg: string, meta?: object)
  • logger.fatal(msg: string, meta?: object)
  • logger.setLevel(level: LogLevel)
  • logger.getLevel(): LogLevel
  • logger.enable()
  • logger.disable()
  • logger.isEnabled(): boolean
  • logger.setSSROnly(ssrOnly: boolean)

Types

type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal";

Configuration

You can configure the logger using the following methods:

// Set the log level
logger.setLevel('debug');

// Enable or disable logging
logger.enable();
logger.disable();

// Set SSR-only mode
logger.setSSROnly(true);

Environment Detection

nexlog automatically detects the current environment (server, browser, or edge) and adjusts its output accordingly. You can also use helper functions for detection:

import { isServer, isNextEdgeRuntime } from 'nexlog';

if (isServer) {
  console.log('Running on server');
} else if (isNextEdgeRuntime) {
  console.log('Running on edge runtime');
} else {
  console.log('Running in browser');
}

Development

To install dependencies:

npm install
# or
yarn install
# or
bun install

To run tests:

npm test
# or
yarn test
# or
bun test

Contributing

While I'm currently the sole developer of this project, I'm open to contributions. If you have suggestions or want to contribute, please feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License.

Support

If you find nexlog helpful, consider sponsoring me. Your support helps me maintain and improve this project.

FAQ

Does nexlog have any external dependencies?

No, nexlog is designed to be completely self-contained with zero external dependencies.

Does nexlog have any side effects?

No, nexlog is carefully designed to avoid any side effects. It doesn't modify global objects or interfere with other parts of your application.

Why is nexlog distributed as TypeScript files?

Distributing nexlog as TypeScript files provides maximum flexibility for users. It allows for better tree-shaking, gives users full type information, and lets them compile the library according to their project's specific needs.

Why do I need to add 'use client' when using useLogger?

The useLogger hook is a React hook, and hooks can only be used in client components. Adding 'use client' at the beginning of the file tells Next.js that this component should be rendered on the client side.

Why do I need to configure Next.js to transpile nexlog?

nexlog is distributed as TypeScript files to provide maximum flexibility and type safety. However, Next.js doesn't automatically transpile dependencies. By adding nexlog to the transpilePackages array in your Next.js configuration, you ensure that the TypeScript files are properly compiled for use in your Next.js application.

I'm getting a "Module parse failed: Unexpected token" error. How do I fix it?

This error typically occurs when Next.js is trying to parse the TypeScript files directly. To resolve this, make sure you've properly configured Next.js to transpile nexlog as described in the "Configuration with Next.js" section above.

The useLogger hook is not working. What might be the issue?

If you're encountering issues with the useLogger hook, ensure that:

  1. You've wrapped your application with the LoggerProvider in your root layout.
  2. You're using 'use client' directive in the file where you're using useLogger.
  3. You've properly configured Next.js to transpile nexlog.

If you're still having issues, please open an issue on the GitHub repository with details about your setup and the specific error you're encountering.