1.9.30 • Published 6 months ago

@voicenter-team/socketio-storage-logger v1.9.30

Weekly downloads
-
License
ISC
Repository
-
Last release
6 months ago

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

ParameterDescription
socketSocketIO connection / undefined default: undefined. Defines the initialized socket socket-io connection.
urlstring / undefined default: undefined. Defines the socket connection url.
socketOptionsobject. Defines initializing configuration options for socket-io connection.
loggerOptionsLoggerOptions. Defines the configuration options for logger.

LoggerOptions interface

ParameterDescription
logToConsoleboolean default: true. This defines if logger should contain default behavior like logging data to console.
overloadGlobalConsoleboolean default: false. This defines if the global console object should be overloaded with logger functionality.
namespacestring. This defines the namespace for the storage key. This value should be unique across the projects.
socketEmitIntervalnumber 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
    }
})
1.9.30

6 months ago

1.9.29

6 months ago

1.9.27

6 months ago

1.9.26

6 months ago

1.9.25

6 months ago

1.9.24

6 months ago

1.9.23

6 months ago

1.9.22

6 months ago

1.9.21

6 months ago

1.9.19

9 months ago

1.9.18

9 months ago

1.9.17

1 year ago

1.9.16

1 year ago

1.9.15

1 year ago

1.9.14

1 year ago

1.9.13

1 year ago

1.9.12

1 year ago

1.9.11

1 year ago

1.9.10

1 year ago

1.9.9

1 year ago

1.9.8

1 year ago

1.9.1

1 year ago

1.9.0

1 year ago

1.9.7

1 year ago

1.9.5

1 year ago

1.9.4

1 year ago

1.9.3

1 year ago

1.9.2

1 year ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.0

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago