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

@voicenter-team/socketio-storage-logger

v1.9.17

Published

Logger that allows you to save logged information into your storage and emit stored logs via socket.io

Downloads

1,439

Readme

socketio-storage-logger

socketio-storage-logger is a library that allows you to log data into the storage and periodically emit stored logs to the server using socket.io-client library functionality.

By default it logs data to the localstorage but you can use your own storage by providing methods for getting and setting data to it.

It also allows you to overload the global console logging methods (like log, warn, error etc.) the by passing appropriate property to the logger configuration.

Install with NPM

npm install @voicenter-team/socketio-storage-logger --save

Configuration parameters

Parameter | Description | --- | --- | socket | SocketIO connection / undefined default: undefined. Defines the initialized socket socket-io connection. url | string / undefined default: undefined. Defines the socket connection url. socketOptions | object. Defines initializing configuration options for socket-io connection. loggerOptions | LoggerOptions. Defines the configuration options for logger.

LoggerOptions interface

Parameter | Description | --- | --- | logToConsole | boolean default: true. This defines if logger should contain default behavior like logging data to console. overloadGlobalConsole | boolean default: false. This defines if the global console object should be overloaded with logger functionality. namespace | string. This defines the namespace for the storage key. This value should be unique across the projects. socketEmitInterval | number default: 60000. This defines the interval for sending logs using sockets in milliseconds. getItem | (storage: string) => string;. This defines the custom function for getting logs from storage. Function can be both synchronous or asynchronous. setItem | (storage: string, logs: string) => void;. This defines the custom function for setting logs to storage. The second 'logs' parameter should be string or stringified object. Function can be both synchronous or asynchronous. parseLog | (level: string, logs: any[]) => string;. This defines the custom function for parsing logs. Function should be synchronous.

Usage

To start using the library first of all import it:

const StorageLogger = require("@voicenter-team/socketio-storage-logger")

or

import StorageLogger from "@voicenter-team/socketio-storage-logger"

When initializing logger you should pass to parameters either url and socketOptions (which will be used for socket connection) or already initialized socket connection socket

Here is an example of creating logger based on url and socketOptions parameters:

const logger = new StorageLogger({
    url: "https://your-server-domain.com",
    socketOptions: {
        reconnection: true,
        reconnectionDelay: 5000,
        reconnectionAttempts: 10,
        perMessageDeflate: false,
        upgrade: false,
        transports: [ 'websocket' ],
        debug: false
    },
    loggerOptions: {
        namespace: "test-project",
        logToConsole: true,
        overloadGlobalConsole: false,
        socketEmitInterval: 10000
    }
})

And here is an example of creating logger based on socket parameter:

import io from "socket.io-client"

const url = "https://your-server-domain.com"
const options: {
    reconnection: true,
    reconnectionDelay: 5000,
    reconnectionAttempts: 10,
    perMessageDeflate: false,
    upgrade: false,
    transports: [ 'websocket' ],
    debug: false
}
const socket = io(url, options)

const logger = new StorageLogger({
    socket,
    loggerOptions: {
        namespace: "test-project",
        logToConsole: true,
        overloadGlobalConsole: false,
        socketEmitInterval: 10000
    }
})

To use custom storage you should provide getter (getItem) and setter (setItem) for your storage. Next example shows usage of custom storage on the example of node-storage:

const StorageLogger = require("@voicenter-team/socketio-storage-logger")
const Storage = require('node-storage');

const store = new Storage('./logs.json');

function getItem(storageId) {
    return store.get(storageId)
}

function setItem(storageId, logs) {
    store.put(storageId, logs);
}

function parseLog (level, logs) {
    const message = logs.map(log => JSON.stringify(log)).join(' ')
    const time = new Date().toISOString()
    return JSON.stringify({ level, time, message })
}

const logger = new StorageLogger({
    url: "https://your-server-domain.com",
    socketOptions: {
        reconnection: true,
        reconnectionDelay: 5000,
        reconnectionAttempts: 10,
        perMessageDeflate: false,
        upgrade: false,
        transports: [ 'websocket' ],
        debug: false
    },
    loggerOptions: {
        namespace: "test-project",
        logToConsole: true,
        overloadGlobalConsole: false,
        socketEmitInterval: 10000,
        getItem,
        setItem,
        parseLog,
    }
})

logger.log("Test log method")
logger.warn("Test warn method")
logger.error("Test error method")

// Next lines do the same that previuos few as we passed config parameter overloadGlobalConsole as true
console.log("Test log method")
console.warn("Test warn method")
console.error("Test error method")

Usage in Chrome Extension project

First of all import AsyncStorageLogger:

  import StorageLogger from "@voicenter-team/socketio-storage-logger"

localStorage is not available in background.js so let's create custom getItem and setItem functions:

 import StorageLogger from "@voicenter-team/socketio-storage-logger"

const getItemFunc = async (storageId) => {
    const results = await chrome.storage.local.get(storageId)
    return results[storageId]
}

const setItemFunc = async (storageId, logs) => {
    await chrome.storage.local.set({ [storageId]: logs })
}

const logger = new StorageLogger({
    url: "https://your-server-domain.com",
    socketOptions: {
        reconnection: true,
        reconnectionDelay: 5000,
        reconnectionAttempts: 10,
        perMessageDeflate: false,
        upgrade: false,
        transports: [ 'websocket' ],
        debug: false
    },
    loggerOptions: {
        namespace: "extension-project",
        logToConsole: true,
        overloadGlobalConsole: false,
        socketEmitInterval: 10000,
        getItem: getItemFunc,
        setItem: setItemFunc
    }
})