Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@sprucelabs/log

Package Overview
Dependencies
Maintainers
9
Versions
350
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@sprucelabs/log

Sprucebot Logger

  • 1.3.0
  • npm
  • Socket score

Version published
Weekly downloads
42
increased by31.25%
Maintainers
9
Weekly downloads
 
Created
Source

Sprucebot Logger

An isomorphic logger (based on iso-log) and metrics collection utilities for the Sprucebot platform.

Installation

yarn add @sprucelabs/log

Usage

In browser console:

Console

And in terminal:

Terminal

Features

  • Works both client and server side

  • Log levels

  • Outputs logs using native console methods

  • Trace log statements to files and lines

  • Sourcemap support

  • Collect and send metrics

Browser (client) Installation

const log = require('@sprucelabs/log');

NodeJS (server) Installation

const logger = require('@sprucelabs/log');
const log = logger.log;

Usage

// After the logger is initialized, set some options
log.setOptions({
	level: 'debug',
	useTrace: true,
	useSourcemaps: true,
	appName: 'myskill', // You choose an app name. This will bucket your metrics together
	appEnv: 'production' // Set the current environment which will allow us to segment metrics from different environments.
	// SERVER OPTION ONLY! Your app key should only be set when using this from your server. This should be treated like a password and NEVER exposed publicly
	appKey: 'ec858f58-0978-4fa7-8cdb-704902f30692'
	// Optionally set the following parameters
	flushAt: 10, // The number of metrics to collect before sending them (default 10)
	flushIntervalSec: 10, // How often to send metrics (if any have been collected) (default 10)
	userAgent: navigator.userAgent(), // The user agent making the request
	packageName: 'myskill', // Your app's package name
	packageVersion: 'v1.4.34', // Your apps's package version
	metricsUrl: 'https://metrics.sprucebot.com' // The server where metrics are sent
});

log.debug('All set!');

log.metric({
	event: 'user-did-something', // The event name to track
	value: 3, // Optional: If set, must be a number
	// All the following parameters are optional and allows you to override what was automatically detected or set in log.setOptions()
	userAgent: navigator.userAgent(), // The user agent making the request
	packageName: 'myskill', // Your app's package name
	packageVersion: 'v1.4.34', // Your apps's package version
	time: Date.now(), // The unix timestamp for this event (default is now)
	hostname: window.location.hostname // The current hostname
})

Timers

This library also provides a couple helper functions for timers:

const startTime = log.timerStart();
// Do some stuff
const milliseconds = log.timerEnd(startTime);
log.metric({
	event: 'timeToDoSomething',
	value: milliseconds
});

Additional NodeJS (server) Features

Server Metrics Collection

We can collect server metrics like CPU and memory usage automatically. After you've initialized the logger and called setOptions() just call:

logger.nodeMetrics();

Request Metrics (for express-based apps)

We can collect stats on the type of HTTP requests being made by installing the request middleware. After you've initialized the logger and called setOptions() initialize your express app and the add the middleware:

const app = express();

app.use(logger.middleware.requests());

Sequelize Middleware Metrics Collection

We can collect stats on create/update/delete methods of models. After you've initialized the logger and called setOptions() initialize Sequelize and add the metrics hooks:

const sequelize = new Sequelize(this.uri, this.options);

logger.sequelizeHooks(sequelize);

Options

level - The log level to use. Default: 'debug'

Valid levels are:

trace
debug
log
info
warn
error
superInfo

If the level specified is info, then info, warn, error, and superInfo logs would be written to the console. trace and debug logs would NOT be written to the console.

useTrace - Whether to run a trace which will add the file and line number. Default: true

useSourcemaps - Whether to try to resolve the original file and line number. Will look for the sourcemap in the corresponding .map file. For example, /some/js/file.js.map. Default: true

Logging Examples

log.trace('log at level trace');
log.debug('log at level debug');
log.log('log at level log');
log.info('log at level info');
log.warn('log at level warn');
log.error('log at level error');
log.superInfo('log at level error');

log.crit('log at level error'); // alias of 'error'
log.fatal('log at level error'); // alias of 'error'

// Anything that can be passed to console.log can be passed to the logger
log.debug({ some: 'object here' });
log.debug('multiple things', 'getting logged here', { some: 'object here' });

Sourcemaps and Webpack

For source maps to properly work you'll need to make sure you're generating them (with the original source info). If you're using webpack you can add this to your config:

{
	devtool: 'cheap-module-source-map';
}

If you're using webpack and receive a Module not found: Error: Can't resolve 'fs', just add the following to your webpack config:

node: {
	fs: 'empty';
}

FAQs

Package last updated on 20 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc