8000 Initial release by dominicsayers · Pull Request #1 · dominicsayers/loglevel · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.ruby-gemset
.ruby-version
Gemfile.lock

*.rbc
capybara-*.html
.rspec
/log
/tmp
/db/*.sqlite3
Expand All @@ -27,9 +27,6 @@ config/secrets.yml
/.bundle
/vendor/bundle

# these should all be checked in to normalize the environment:
# Gemfile.lock, .ruby-version, .ruby-gemset

# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
.rvmrc

Expand Down
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
6 changes: 5 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
---
AllCops:
TargetRubyVersion: 1.9
TargetRubyVersion: 2.0
DisplayCopNames: true
Exclude:
- 'tmp/**/*'

Style/MixinGrouping:
Exclude:
- 'spec/**/*.rb'

Metrics/BlockLength:
CountComments: false # count full line comments?
Exclude:
Expand Down
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
6870 Expand Up @@ -5,10 +5,8 @@ rvm:
- 2.4.0
- 2.3
- 2.2
- 1.9
- jruby

- 2.1
- 2.0
before_install:
- gem update bundler

script: bundle exec rspec
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

source 'https://rubygems.org'
gemspec
ruby RUBY_VERSION

group :development do
gem 'bundler'
Expand Down
127 changes: 0 additions & 127 deletions Gemfile.lock

This file was deleted.

77 changes: 74 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
# loglevel
## Loglevel

[![Gem version](https://badge.fury.io/rb/loglevel.svg)](https://rubygems.org/gems/loglevel)
[![Gem downloads](https://img.shields.io/gem/dt/loglevel.svg)](https://rubygems.org/gems/loglevel)
[![Build Status](https://travis-ci.org/dominicsayers/loglevel.svg?branch=master)](https://travis-ci.org/dominicsayers/loglevel)
[![Code quality](http://img.shields.io/codeclimate/github/dominicsayers/loglevel.svg?style=flat)](https://codeclimate.com/github/dominicsayers/loglevel)
[![Coverage Status](https://coveralls.io/repos/github/dominicsayers/loglevel/badge.svg?branch=master)](https://coveralls.io/github/dominicsayers/loglevel?branch=master)
[![Dependency Status](https://gemnasium.com/badges/github.com/dominicsayers/loglevel.svg)](https://gemnasium.com/github.com/dominicsayers/loglevel)
[![Security](https://hakiri.io/github/dominicsayers/loglevel/master.svg)](https://hakiri.io/github/dominicsayers/loglevel/master)

Control logging at runtime with an environment variable

Usage:
Expand All @@ -19,13 +28,75 @@ LOGLEVEL=DEBUG,NOAR,NOHTTP rails server
would set the Rails logger level to `:debug` but would suppress messages from
the ActiveRecord logger and the HttpLogger gem.

The full range of features is itemized if you use the `HELP` option:
The features are itemized if you use the `HELP` option:

```sh
LOGLEVEL=HELP rails server
LOGLEVEL=HELP rails console
```

Here are the available settings:

| Option | Description |
| --------- | ------------------------------------------- |
| HELP | Show these options |
| FATAL | Equivalent to config.log_level = :fatal |
| ERROR | Equivalent to config.log_level = :error |
| WARN | Equivalent to config.log_level = :warn |
| INFO | Equivalent to config.log_level = :info |
| DEBUG | Equivalent to config.log_level = :debug |
| NOAR | Do not show ActiveRecord messages |
| NOHTTP | Do not show HTTP messages |
| NOHEADERS | Do not include response headers in HTTP log |
| NOBODY | Do not include response body in HTTP log |

### Dependencies

The examples in this document assume Loglevel is being used in a Rails
environment but it doesn't depend on Rails and can be used in other contexts.

There are specific options to handle Railsy logging scenarios: things like
controlling ActiveRecord logging. There are also specific options for handling
the HttpLogger gem.

### Logger

By default Loglevel will instantiate Ruby's default Logger class. If you want to
use a different logger then you can use an environment variable to tell Loglevel
which logger you use:

```sh
LOGLEVEL_LOGGER=Log4r LOGLEVEL=DEBUG rails server
```

### Log device

By default Loglevel will setup logging to the `STDOUT` device. If you want to
use a different device there's an environment variable for that:

```sh
LOGLEVEL_DEVICE=tmp/test.log LOGLEVEL=DEBUG rails server
```

### Classes with a logger

By default, Loglevel will setup the logger for Rails, ActiveRecord::Base and
HttpLogger if they are present.

It will also setup logging for any other classes that you include in an
environment variable:

```sh
LOGLEVEL_CLASSES=MyClass LOGLEVEL=DEBUG rails server
```

The only methods the class must support are `logger` and `logger=`

### Contributing

[![Developer](http://img.shields.io/badge/developer-awesome-brightgreen.svg?style=flat)](https://www.dominicsayers.com)

1. Fork it
1. Create your feature branch (`git checkout -b my-new-feature`)
1. Commit your changes (`git commit -am 'Add some feature'`)
1. Push to the branch (`git push origin my-new-feature`)
1. Create new Pull Request
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ begin

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
rescue LoadError
# no rspec available
puts 'rspec is not available'
end
4 changes: 1 addition & 3 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
---
machine:
ruby:
version: 1.9.3
version: '2.0'
dependencies:
pre:
- echo "export rvm_ignore_gemsets_flag=1" >> ~/.rvmrc
- gem update --system
- gem install bundler
override:
- bundle check --path=vendor/bundle || bundle install --path=vendor/bundle --jobs=4 --retry=3 --full-index
test:
override:
- RAILS_ENV=test bundle exec rspec -r rspec_junit_formatter --format RspecJunitFormatter -o $CIRCLE_TEST_REPORTS/rspec/junit.xml:
Expand Down
90 changes: 90 additions & 0 deletions lib/loglevel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'loglevel/classes'
require 'loglevel/active_record'
require 'loglevel/http_logger'
require 'loglevel/help'

module Loglevel
ENV_VAR_LEVEL = 'LOGLEVEL'.freeze
ENV_VAR_LOGGER = 'LOGLEVEL_LOGGER'.freeze
ENV_VAR_DEVICE = 'LOGLEVEL_DEVICE'.freeze
ENV_VAR_CLASSES = 'LOGLEVEL_CLASSES'.freeze
LOG_LEVELS = %w(DEBUG INFO WARN ERROR FATAL UNKNOWN).freeze

class << self
attr_reader :classes
attr_writer :logger

def setup
return unless ENV[ENV_VAR_LEVEL]

@classes = Loglevel::Classes.new.classes
@settings = nil
@logger = nil
@log_level = nil

setup_active_record
setup_http_logger
setup_remaining_classes

help if help?
end

def logger
@logger ||= logger_class.new log_device
end

def log_level
@log_level ||= logger_class.const_get log_level_name
end

private

include Loglevel::ActiveRecord
include Loglevel::HttpLogger
include Loglevel::Help

# Setup any other classes (e.g. Rails)
def setup_remaining_classes
classes.each do |klass|
klass.logger = logger
klass.logger.level = log_level
end
end

def log_level_name
(LOG_LEVELS & settings).first || 'INFO'
end

def logger_class
Object.const_get ENV.fetch(ENV_VAR_LOGGER, 'Logger')
end

def log_device
%w(STDOUT STDERR).include?(log_device_name) ? Object.const_get(log_device_name) : log_device_name
end

def log_device_name
ENV.fetch(ENV_VAR_DEVICE, 'STDOUT')
end

# Implementing as a method in case someone wants to detect the OS and make
# an OS-dependent null device
def null_device
@null_device ||= '/dev/null'
end

def null_logger
@null_logger ||= logger_class.new null_device
end

def settings
@settings ||= ENV.fetch(ENV_VAR_LEVEL, '').upcase.split(',')
end

def lookup(setting)
settings.include?(setting)
end
end
end

Loglevel.setup
Loading
0