File: README.md

package info (click to toggle)
ruby-logging 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 660 kB
  • sloc: ruby: 6,139; sh: 11; makefile: 2
file content (138 lines) | stat: -rw-r--r-- 4,252 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
## Logging
by Tim Pease [![](https://secure.travis-ci.org/TwP/logging.svg)](https://travis-ci.org/TwP/logging)

* [Homepage](http://rubygems.org/gems/logging)
* [Github Project](https://github.com/TwP/logging)

### Description

**Logging** is a flexible logging library for use in Ruby programs based on the
design of Java's log4j library. It features a hierarchical logging system,
custom level names, multiple output destinations per log event, custom
formatting, and more.

### Installation

```
gem install logging
```

### Examples

This example configures a logger to output messages in a format similar to the
core ruby Logger class. Only log messages that are warnings or higher will be
logged.

```ruby
require 'logging'

logger = Logging.logger(STDOUT)
logger.level = :warn

logger.debug "this debug message will not be output by the logger"
logger.warn "this is your last warning"
```

In this example, a single logger is created that will append to STDOUT and to a
file. Only log messages that are informational or higher will be logged.

```ruby
require 'logging'

logger = Logging.logger['example_logger']
logger.level = :info

logger.add_appenders \
    Logging.appenders.stdout,
    Logging.appenders.file('example.log')

logger.debug "this debug message will not be output by the logger"
logger.info "just some friendly advice"
```

The Logging library was created to allow each class in a program to have its
own configurable logger. The logging level for a particular class can be
changed independently of all other loggers in the system. This example shows
the recommended way of accomplishing this.

```ruby
require 'logging'

Logging.logger['FirstClass'].level = :warn
Logging.logger['SecondClass'].level = :debug

class FirstClass
  def initialize
    @logger = Logging.logger[self]
  end

  def some_method
    @logger.debug "some method was called on #{self.inspect}"
  end
end

class SecondClass
  def initialize
    @logger = Logging.logger[self]
  end

  def another_method
    @logger.debug "another method was called on #{self.inspect}"
  end
end
```

There are many more examples in the [examples folder](/examples) of the logging
package. The recommended reading order is the following:

* [simple.rb](/examples/simple.rb)
* [rspec_integration.rb](/examples/rspec_integration.rb)
* [loggers.rb](/examples/loggers.rb)
* [classes.rb](/examples/classes.rb)
* [hierarchies.rb](/examples/hierarchies.rb)
* [names.rb](/examples/names.rb)
* [lazy.rb](/examples/lazy.rb)
* [appenders.rb](/examples/appenders.rb)
* [layouts.rb](/examples/layouts.rb)
* [reusing_layouts.rb](/examples/reusing_layouts.rb)
* [formatting.rb](/examples/formatting.rb)
* [colorization.rb](/examples/colorization.rb)
* [fork.rb](/examples/fork.rb)
* [mdc.rb](/examples/mdc.rb)

### Extending

The Logging framework is extensible via the [little-plugger](https://github.com/twp/little-plugger)
gem based plugin system. New appenders or formatters can be released as ruby
gems. When installed locally, the Logging framework will automatically detect
these gems as plugins and make them available for use.

The [logging-email](https://github.com/twp/logging-email) plugin is a good
example to follow. It includes a [`lib/logging/plugins/email.rb`](https://github.com/twp/logging-email/tree/master/lib/logging/plugins/email.rb)
file which is detected by the plugin framework. This file declares a
`Logging::Plugins::Email.initialize_email` method that is called when the plugin
is loaded.

The three steps for creating a plugin are:

* create a new Ruby gem: `logging-<name>`
* include a plugin file: `lib/logging/plugins/<name>.rb`
* definie a plugin initializer: `Logging::Plugins::<Name>.initialize_<name>`

### Development

The Logging source code relies on the Mr Bones project for default rake tasks.
You will need to install the Mr Bones gem if you want to build or test the
logging gem. Conveniently there is a bootstrap script that you can run to setup
your development environment.

```
script/bootstrap
```

This will install the Mr Bones gem and the required Ruby gems for development.
After this is done you can rake `rake -T` to see the available rake tasks.

### License

The MIT License - see the [LICENSE](/LICENSE) file for the full text.