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

@cp949/web-logger-react

v1.0.3

Published

React hooks for @cp949/web-logger

Downloads

113

Readme

@cp949/web-logger-react

React hooks for @cp949/web-logger.

Languages: English | 한국어

Installation

pnpm add @cp949/web-logger-react @cp949/web-logger

or

npm install @cp949/web-logger-react @cp949/web-logger

or

yarn add @cp949/web-logger-react @cp949/web-logger

Requirements

  • React 18 or 19
  • @cp949/web-logger (peer dependency)

Usage

Basic Usage

import { useWebLogger } from '@cp949/web-logger-react';
import { useEffect } from 'react';

function UserList() {
  const logger = useWebLogger('[UserList]');

  useEffect(() => {
    logger.info('hello users'); // => [UserList] hello users
  }, [logger]);

  return <div>UserList</div>;
}

Without Prefix

import { useWebLogger } from '@cp949/web-logger-react';
import { useEffect } from 'react';

function App() {
  const logger = useWebLogger();

  useEffect(() => {
    logger.debug('Application started');
    logger.info('User logged in', { userId: 123 });
    logger.warn('Rate limit approaching');
    logger.error('Failed to fetch data', error);
  }, [logger]);

  return <div>App</div>;
}

Multiple Components

import { useWebLogger } from '@cp949/web-logger-react';

function UserList() {
  const logger = useWebLogger('[UserList]');

  useEffect(() => {
    logger.info('hello users'); // => [UserList] hello users
  }, [logger]);

  return <div>UserList</div>;
}

function UserDetails() {
  const logger = useWebLogger('[UserDetails]');

  useEffect(() => {
    logger.info('user info'); // => [UserDetails] user info
  }, [logger]);

  return <div>UserDetails</div>;
}

Next.js / SSR-friendly usage

// app/(client)/page.tsx (Next.js App Router)
'use client';

import { useEffect } from 'react';
import { useWebLogger } from '@cp949/web-logger-react';

export default function Page() {
  const logger = useWebLogger('[Page]');

  useEffect(() => {
    logger.info('mounted in browser');
  }, [logger]);

  return <div>Hello</div>;
}

Use useWebLogger only in client components / browser contexts. For server-side logging, use your server logger or guard with if (typeof window !== 'undefined').

Server-side note

  • Prefer your platform logger (e.g., console, pino, winston) on the server.
  • Keep @cp949/web-logger client-side to avoid bundling browser-only code into SSR lambdas.

Why this hook?

  • Global state compatibility: Uses the shared webLogger (and createPrefixedLogger) so runtime log level / sensitive key & pattern changes stay in sync.
  • React-friendly memoization: useMemo avoids recreating instances on re-render; dependency on prefix makes intent explicit.
  • Safe defaults for app code: In client components, you can import the hook and get a logger that respects global settings without wiring anything else.
  • Default prefix: If you omit prefix, the shared webLogger is returned (default prefix [APP]).
  • Pattern warnings: Suppress default-pattern warnings via setSensitivePatternWarnings(true) in @cp949/web-logger if you intentionally replace built-ins.

More options (sensitive keys/patterns, warnings): see packages/web-logger/README.md in this repo or the npm docs for @cp949/web-logger.

Static prefix and client-only code? Declaring const logger = createPrefixedLogger('[App]') outside components is fine, but keep it in client-only modules to avoid pulling browser code into SSR bundles.

API

useWebLogger(prefix?: string)

Creates a WebLogger instance with optional prefix. The logger instance is memoized and only recreated when the prefix changes.

Parameters

  • prefix (optional): A string prefix to prepend to all log messages (e.g., '[UserList]')

Returns

A WebLogger instance with the following methods:

  • debug(message?: unknown, ...optionalParams: unknown[]): void
  • info(message?: unknown, ...optionalParams: unknown[]): void
  • warn(message?: unknown, ...optionalParams: unknown[]): void
  • error(message?: unknown, ...optionalParams: unknown[]): void
  • log(...args: unknown[]): void
  • group(title: string, data?: LogMetadata): void
  • groupEnd(): void
  • time(label: string): void
  • timeEnd(label: string): void
  • setLogLevel(level: LogLevel): void
  • get currentLogLevel(): LogLevel
  • get isEnabled(): boolean

For more details about the WebLogger API, see the @cp949/web-logger documentation.

Example

import { useWebLogger } from '@cp949/web-logger-react';

function MyComponent() {
  const logger = useWebLogger('[MyComponent]');

  useEffect(() => {
    // All logs will be prefixed with [MyComponent]
    logger.debug('Component mounted');
    logger.info('Data loaded', { count: 10 });
    logger.warn('Warning message');
    logger.error('Error occurred', error);
  }, [logger]);

  return <div>My Component</div>;
}

Features

  • Memoized Logger: The logger instance is memoized and only recreated when the prefix changes
  • TypeScript Support: Full TypeScript definitions included
  • React 18 & 19 Compatible: Works with both React 18 and 19
  • Zero Configuration: Works out of the box with @cp949/web-logger

License

MIT

Related