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

@financial-times/n-auto-logger

v4.1.2

Published

an opinionated [enhancer](https://github.com/Financial-Times/n-express-enhancer) to log function calls in the operation/action model > using [n-logger](https://github.com/Financial-Times/n-logger) by default, can be set to any logger instance

Downloads

6

Readme

n-auto-logger

an opinionated enhancer to log function calls in the operation/action model

using n-logger by default, can be set to any logger instance

npm version npm download node version gitter chat

CircleCI Coverage Status Known Vulnerabilities Scrutinizer Code Quality Dependencies devDependencies

Install

npm install @financial-times/n-auto-logger

Usage

log an Action

automatically log the start, success/failure state with necessary metadata including function name as action, it can be applied to both individual action function or an action function bundle.

import { logAction, tagService, compose } from '@financial-times/n-auto-logger';

const result = logAction(someFunction)({...params, meta}); // action function

export default compose(
 tagService('service-name'), // optional
 logAction,
)({ 
 methodA, 
 methodB, 
 methodC 
}); // action function bundle

more details on action function

log an Operation

automatically log the start, success/failure state with necessary metadata including function name as operation, it can be applied to both individual operation function or an operation function bundle.

import { logOperation, errorToHanlder, compose } from '@financial-times/n-auto-logger';

const operationFunction = (meta, req, res) => {}; // operation function
const someMiddleware = compose(
 errorToHandler, 
 logOperation
)(operationFunction);

export default compose(
 errorToHandler, 
 logOperation
)({ 
 operationFunctionA, 
 operationFuncitonB 
}); // operation function bundle

more details on operation function

auto log level

set auto log level in .env with 3 options: verbose(default) | concise | error.

AUTO_LOG_LEVEL=concise
  • verbose would log every stage(start, success/failure) of operation and action
  • concise would log success/failure of operation and only failure of action
  • error would log only failure of operation and action

You can override the ENV_VAR with a flag value as well if you would like to switch log level in production for debugging.

mute logger fields

set key names of fields to be muted in .env to reduce log for development or filter fields in production.

LOGGER_MUTE_FIELDS=transactionId, userId

customise logger instance

import { setupLoggerInstance } from '@financial-times/n-auto-logger';
import winston from 'winston'; // you can use any instance as long as it has .info, .warn, .error method

setupLoggerInstance(winston); // set the logger instance before using the enhancer

requestId

use the requestIdMiddleware to ensure the logs are easy to thread when debugging, and this would work well with n-api-factory to pass it to up stream services.

import { requestIdMiddleware } from '@financial-times/n-auto-logger';

// you might want to exclude `__*` path from log
app.use(/^\/(?!_{2}).*$/, [
 // use it before any other middleware to be logged
 requestIdMiddleware,
 //...other middlewares
]);

Gotcha

reserved fields

n-auto-logger will append values to following reserved fields automatically, the values would be overriden by the key value of the same name in your args/params/meta or error object, be cautious not to override them unintentionally.

| fields | default | convention used in | |-----------|------------------------------------------------------------------------------------|------------------------| | operation | operationFunction.name | n-auto-logger/metrics | | action | actionFunction.name | n-auto-logger/metrics | | service | tagService('service-name') | n-auto-metrics | | result | 'success''failure' | n-auto-logger | | category | 'FETCH_RESPONSE_ERROR''FETCH_NETWORK_ERROR''NODE_SYSTEM_ERROR''CUSTOM_ERROR' | n-auto-metrics/n-error | | type | specify the unique error type for debugging and error handling | n-auto-metrics/n-error | | status | recorded for service action call failure | n-error/n-api-factory | | stack | error stack trace | n-error |

ignored fields

user, handler, _locals fields from error or meta object would not be logged by default.

sensitive personal data could be put in meta.user and would not be logged

const meta = { ...meta, user: { id, email } };

UI but not debugger facing data could be put in error.user and would not be logged e.g. app error status (> API call error status), user message (> error message from API for debugging)

throw nError({ status, message }).extend({ user: { status, message } });

.handler field would not be logged, as it is only useful for error handler

throw nError({ status: 404 }).extend({ handler: 'REDIRECT_TO_INDEX' });

_locals field would not be logged as it is verbose and not relevant to debug

// in case you didn't clone the error object in error handler
function(err, req, res, next) {
  const e = {...err}; // clone the error object to avoid mutate the input
  res.render('template', e); // res.render is not a pure function, it would assign _locals to e
}

error auto parse

n-auto-logger would parse different forms of the following error objects to logger-suitable format automatically(detail), while it still logs plain object and string message.

  • Fetch Response Error content-type:application/json,text/plain,text/html
  • Fetch (Network) Error
  • Node native Error objects
  • Custom objects extends native Error object
  • NError

log auto trim

n-auto-logger would trim any empty fields and method fields in the input meta or error objects automatically to concise log (detail), you shouldn't be concerned about passing excessive meta fields or extend Error object with methods.

mute logger in test

stub the logger instance instead of the whole module

import logger from '@financial-times/n-auto-logger'; // the underlying logger instance (`n-logger`)

// sinon sandbox
sandbox.stub(logger);

// jest
logger.info = jest.fn();
logger.warn = jest.fn();
logger.error = jest.fn();

Licence

MIT