Giter VIP home page Giter VIP logo

cx_log's Introduction

CxLog

Trying to debug an issue by looking at multiple logs is often cumbersome. CxLog allows you to create a single log line for a context (request, background job, etc.) in your application.

Installation

Install the gem and add to the application's Gemfile by executing:

    $ bundle add cx_log

If bundler is not being used to manage dependencies, install the gem by executing:

    $ gem install cx_log

Usage

Basic Usage

CxLog generates structured logs. All logs will have a message attribute (that can be overwritten) using CxLog.add. In order to add an attribute the log, use CxLog.add:

    CxLog.log(message: 'test', foo: 'bar')

Flushing the log will generate {"message":"test","foo":"bar"}.

CxLog supports multiple values per key:

    CxLog.log(message: 'test', foo: 'bar')
    # some code
    CxLog.add(foo: 'baz')

Flushing the log will generate {"message":"test","foo":["bar","baz"]}.

Rails

If you are using Rails, you can use the CxLog middleware to automatically generate context logs.

Add the following to your config/application.rb:

    config.middleware.use CxLog::Middleware

Log Format

By default, CxLog generates logs in JSON format. You can use your own or choose one that is shipped with the gem

  CxLog.options = { 
    formatter: CxLog::Formatters::KeyValueFormatter.new 
  }

If you are using Rails middleware, you can set the formatter in an initializer:

  config.middleware.use CxLog::Middleware, formatter: CxLog::Formatters::KeyValueFormatter.new

Filter Parameters

CxLog supports filtering parameters by default. But, you can overwrite the list of filter parameters by setting the filter_parameters option:

  CxLog.options = { 
    filter_parameters: ['password', 'secret']
  }

If you are using Rails middleware, you can set the filter parameters in an initializer:

  config.middleware.use CxLog::Middleware, filter_parameters: ['password', 'secret']

The log will replace the value of sensitive parameters with [FILTERED]:

{"message":"","request_id":"b41e3733-3ca6-46e9-a968-73e995443ec2",
 "controller":"posts","action":"index","method":"GET","post_count":1,
 "password":"[FILTERED]"}

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/gupta-ankit/cx_log. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the CxLog project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

PS: There is this nice article that I read a few years ago, that has inspired this gem.

cx_log's People

Contributors

gupta-ankit avatar

Stargazers

 avatar

Watchers

 avatar

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.