Bunyan with webpack - trying to access fs which has been shimmed

673 views Asked by At

I am trying to setup bunyan in my webpack based application.

I have the following webpack configuration:

  // these shims are needed for bunyan
        alias: {
            'dtrace-provider': path.resolve('empty-shim.js'),
            "fs": path.resolve('empty-shim.js'),
            'safe-json-stringify': path.resolve('empty-shim.js'),
            "mv": path.resolve('empty-shim.js'),
            'source-map-support': path.resolve('empty-shim.js')
        }

where empty-shim is an empty file

And the following streams defined in my logger:

{
            level: "trace",
            path: "/logs/trace.log"
            // Logging from external libraries used by your app or very detailed application logging.
        },
        {
            level: "debug",
            path: "/logs/debug.log"
            // Anything else, i.e. too verbose to be included in "info" level.
        },
        {
            level: "info",
            path: "/logs/info.log"
            // Detail on regular operation.
        },
        {
            level: "warn",
            path: "/logs/warn.log"
            // A note on something that should probably be looked at by an operator eventually.
        },
        {
            level: "error",
            stream: process.stderr // we pipe error also to stdout
            // Fatal for a particular request, but the service/app continues
            // servicing other requests. An operator should look at this soon(ish).
        },
        {
            level: "error",
            path: "/logs/error.log"
            // Fatal for a particular request, but the service/app continues
            // servicing other requests. An operator should look at this soon(ish).
        },
        {
            level: "fatal",
            path: "/logs/fatal.log"
            // The service/app is going to stop or become unusable now.
            // An operator should definitely look into this soon.
        }

When i try to access my logger however I get this error:

Uncaught TypeError: fs.createWriteStream is not a function
    at Logger.addStream (http://localhost:8080/bundle.js:81080:28)
    at http://localhost:8080/bundle.js:80958:19
    at Array.forEach (native)
    at new Logger (http://localhost:8080/bundle.js:80957:26)
    at Function.createLogger (http://localhost:8080/bundle.js:82060:13)
    at BunyanLogger.getLogger (http://localhost:8080/bundle.js:80479:18)

I assume this is cause fs has been shimmed to an empty file.

How can I get this to work?

1

There are 1 answers

0
Peter Lyons On

I worked around this annoyance by aliasing bunyan-sfdx-no-dtrace as an alternative to bunyan. It's a fork that removes the dtrace stuff.

Here's just the relevant bits of my webpack.config.js

module.exports = {
  resolve: {
    alias: {
      bunyan: "bunyan-sfdx-no-dtrace"
    }
  }
};

Disclaimer AFAICT neither bunyan itself nor bunyan-sfdx-no-dtrace are actively/properly maintained so buyer beware. I had to deal with this because bunyan is a sub-dependency of a module I need.