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

loopback-audit-logger

v0.0.3

Published

Component to add audit trail capability to loopback projects

Downloads

16

Readme

Build Status

loopback-audit-logger

A component to add audit log capability to loopback projects

It's a fork from loopback-audit-trail which does not rely on the loopback context to store and access the current user. Instead it stores currentUser in req. Additionally it fixes an issue with recursive references which now allows to stream the logs to mongoDb and plays well with bunyan-mongodb-logger

This logger works by attaching afterRemote and afterRemoteError methods on an API; this means it would work if API is called as http resource and not as method call. Following are logged as audit log.

  • HTTP method
  • HTTP URL
  • Model name
  • Model method name
  • Arguments as JSON object
    • Request params
    • Request query
    • Request headers
    • Method arguments
  • Method result - This is the return object as specified in returns parameter of method declaration.
  • Method error - In case of errors, it captures the error object as populated by loopback.
  • User as JSON object
    • If user is associated with the current loopback access token then entire object is captured
    • If no accessToken is present then user.name is set to ANONYMOUS
  • User IP address - It is captued in the user object as user.ip

Sample usage with bunyan-mongodb-logger

In your server.js, before calling boot initialize a bunyan-mongodb-logger instance like

var bunyanMongoDbLogger = require('bunyan-mongodb-logger');
var log = bunyanMongoDbLogger({
  name: 'myApp',
  streams: ['stdout', 'mongodb'],
  url: 'mongodb://localhost:27017/logs',
});

Pass the instance of bunyan to loopback-audit-trail as

require('loopback-audit-logger')(log);

Add the following to initialize the component in your component-config.json.

"loopback-audit-logger": {}

Sample usage with node-bunyan

Instead of a bunyan-mongodb-logger istance, initialize a bunyan logger instance like

var bunyan = require('bunyan');
var log = bunyan.createLogger({name: "myApp"});

P.S. You should add the code to initialize logger component with bunyan logger before calling boot method of loopback.

Use custom user model

To use with a custom user model you can attach it in server.js like

app.use(loopback.token());
app.use(function setCurrentUser(req, res, next) {
  if (!req.accessToken) {
    return next();
  }
  app.models.user.findById(req.accessToken.userId, function(err, user) {
    if (err) {
      return next(err);
    }
    if (!user) {
      return next(new Error('No user with this access token was found.'));
    }
    req.currentUser = user.toObject();
    next();
  });
});