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

floating-logger

v1.0.2

Published

A development tool for Next.js and Redux applications that captures and displays logs directly within a floating panel on the app.

Downloads

2

Readme

Floating Logger Dev Tool

A development tool for Next.js and Redux applications that captures and displays logs directly within a floating panel on the app.

screen1

Features

  • Floating Log Display: Shows logs in a floating panel within your application interface.
  • Redux State Viewer: Displays the current Redux state.
  • Backend and Frontend Logging: Capture logs from your API routes as well as the front end components.

Installation

Install the package via npm:

npm install floating-logger

Setup in Next.js

1. Initialize Redux Store and Providers

In your layout.tsx or App.tsx, set up the Redux store and the Floating Logger:

'use client';

import "./globals.css";
import { FloatingLogger, LogProvider, configureLogger } from 'floating-logger';
import { Inter } from "next/font/google";
import { Provider } from 'react-redux';
import store from './redux/store';

const inter = Inter({ subsets: ["latin"] });

configureLogger('myCustomLog'); // Sets a global logging function alias

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <Provider store={store}>
          <LogProvider>
            <FloatingLogger logSelector={(state) => state} />
            {children}
          </LogProvider>
        </Provider>
      </body>
    </html>
  );
}

2. Use Logging in Your Components

Log messages directly from any component:

'use client';

import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { increment } from './redux/store';

const Home = () => {
  const dispatch = useDispatch();
  const count = useSelector((state) => state.counter.count);

  useEffect(() => {
    myCustomLog('info', 'Home page loaded');
  }, []);

  const handleIncrement = () => {
    dispatch(increment());
    myCustomLog('warn', 'Incremented count');
  };

  return (
    <div>
      <h1>Floating Logger Test App</h1>
      <p>Count: {count}</p>
      <button onClick={handleIncrement}>Increment</button>
    </div>
  );
};

export default Home;

3. Backend Logging Setup

Floating Logger API/server logs can also be sent to floating logger. Information from the backend is sent over a Websocket so we can view it directly in our app. You will have to add an api route to allow the floating-logger package to establish the socket connection:

Step 1: Create an API Route to Start the Logger

Add the API route in Next at src/app/api/start-logger-server.ts:

import { NextRequest, NextResponse } from 'next/server';

export async function GET(request: NextRequest) {
  if (typeof window === 'undefined') {
    await import('floating-logger/server'); // Start the WebSocket server
  }

  return NextResponse.json({ message: 'WebSocket server initialized' }, { status: 200 });
}

export async function POST(request: NextRequest) {
  try {
    const { aliasName } = await request.json();

    if (typeof window === 'undefined') {
      const { configureServerLogger } = await import('floating-logger/server');
      configureServerLogger(aliasName);
      return NextResponse.json({ message: 'Logger configured' }, { status: 200 });
    }

    return NextResponse.json({ error: 'Cannot configure logger on the client side' }, { status: 400 });
  } catch (error) {
    return NextResponse.json({ error: 'Failed to configure logger' }, { status: 500 });
  }
}

Step 2: Start and Configure the Logger

  • Start the Logger: Send a GET request to /api/start-logger-server to initialize the WebSocket server.
  • Configure Logger Alias: Send a POST request to /api/start-logger-server with the desired alias name (aliasName) to synchronize the alias with the frontend logger.

Configuring the Alias Name

To ensure consistency between frontend and backend logging, the log function alias name is configured once on the frontend using configureLogger('myCustomLog') or maybe configureLogger('log'). The same alias is then applied to the backend.

Quick Start Commands

  1. Log Information: myCustomLog('info', 'This is an info log');
  2. Log Warnings: myCustomLog('warn', 'This is a warning log');
  3. Log Errors: myCustomLog('error', 'This is an error log');
  4. Log Debug Messages: myCustomLog('debug', 'Debugging log message');