2.0.0 • Published 3 years ago

@firebase-logger/web v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Firebase logger - Get your (mobile) app logs remotely

This library allows you to log data from a web or mobile app to a Firebase Realtime Database, to be able to debug and monitor remotely.

✨ Features

  • Save logs to a Firebase Realtime Database
  • Logs locally in development mode and remotely in production mode
  • Remote and reactive triggerable log level to prevent logs' flooding
  • Supports firebase SDKs (if you are using ReactNative firebase check @firebase-logger/reactnative)
  • Save logs to a user-specific path, to easily find user's logs

🗒️ Table of Contents

Get started

Install the package

npm i @firebase-logger/core @firebase-logger/web or yarn add @firebase-logger/core @firebase-logger/web

Prepare Firebase

If you already have a Firebase setup in your project, you can skip this part.

  1. Create a Firebase project
  2. Create the database with open rules (you can replace them later on with the example rules in the code samples)
  3. Add an app to your project (get help here)
  4. Get the config and initialize Firebase in your code:
import firebase from "firebase/app";

if (!firebase.apps.length) {
  firebase.initializeApp(FIREBASE_CONFIG);
}
  1. Finally, don't forget to create the logger in database that will contain the log level, for instance, add this to the database:
{
  "loggers": {
    "production": {
      "level": "ERROR"
    }
  }
}

Initialize the logger

import logger from "@firebase-logger/web";

logger.init(process.env.NODE_ENV === 'production');

It will initialize the logger that will log remotely only in production mode, under the logs/main path, using the loggers/main You can re-initialize the logger as soon as the user is authenticated to prevent logging everything in the anonymous path, but in the user-specific path. Learn more in the code samples TODO link

It's ready, log messages

It can be used like the standard console object.

import logger from '@firebase-logger/web'

logger.debug('Hello world');
logger.info('Hello world', 42);
logger.warn({ title: 'Hello', subtitle: 'World' });
logger.error(error);
logger.critical('Something bad happened');

API

init

ParameterRequiredDefault valueUsage
shouldLogRemotelyNotrueLogs to Firebase only if set to true, otherwise, uses the standard console methods like console.error
getUserIdNoasync () => 'anonymous'A function that returns a promise containing the user identifier as a string or a number
databaseLoggerPathOrNullNo'loggers/main'The database path to the logger data. Note that this is where the log level is defined (see the sample data)
databaseLogsCollectionOrNullfalse'logs/main'The database path to the logs. It gets created automatically when logging

log

You can use the following methods to log information:

MethodLogs when log level is
debugDEBUG
infoDEBUG, INFO
warnDEBUG, INFO, WARN
errorDEBUG, INFO, WARN, ERROR
criticalDEBUG, INFO, WARN, ERROR, CRITICAL

All these methods accept as many arguments as you want to provide them.

Samples

1.0.1

3 years ago

2.0.0

3 years ago

1.0.0-alpha.3

3 years ago

1.0.0-alpha.2

3 years ago

1.0.0-alpha.1

3 years ago