Giter VIP home page Giter VIP logo

Comments (5)

dougwilson avatar dougwilson commented on May 5, 2024

There is no good way except to define your own custom formatter and just capture the data in there. I was literally just contemplating adding the ability to write objects if the stream you give is an object-mode stream. But IDK.

Your work-around would be as follows:

app.use(logger(function (tokens, req, res) {
  client.log({
    IP_ADDRESS: tokens['remote-addr'](req, res),
    METHOD: tokens['method'](req, res),
    URL: tokens['url'](req, res),
    // ... (you get the idea)
  })
}));

from morgan.

dstroot avatar dstroot commented on May 5, 2024

Doug - you are an animal - not sure how cover everything. Thanks - will give this a try.

Otherwise might give Bunyan a shot. I like the idea of JSON logs.

from morgan.

dougwilson avatar dougwilson commented on May 5, 2024

No problem. I also realized I made a type in the code --- I updated the code above to fix it.

from morgan.

dougwilson avatar dougwilson commented on May 5, 2024

Another possible work-around would be the following code:

// Define your morgan logger to log JSON to your client
// The object here takes your keys and strings that use
// the morgan token format
app.use(logger(format(client, {
  IP_ADDRESS: ':remote-addr',
  METHOD: ':method',
  URL: ':url',
  LENGTH: ':res[content-length]',
  // etc.
})));

function format(client, obj) {
  var keys = Object.keys(obj)
  var token = /^:([-\w]{2,})(?:\[([^\]]+)\])?$/
  return function (tokens, req, res) {
    var data = {}
    for (var i = 0; i < keys.length; i++) {
      var val = token.exec(obj[key])
      data[key] = val !== null
        ? tokens[val[1]](req, res, val[2])
        : obj[key]
    }
    client.log(data)
  }
}

from morgan.

dstroot avatar dstroot commented on May 5, 2024

Doug - I am closing this out. Would love an easy way to send log data as JSON to Loggly but not worth it to me to keep playing with this. Thanks for the FAST responses and great help!

from morgan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.