klogger-logger 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +21 -0
- data/README.md +190 -0
- data/VERSION +1 -0
- data/lib/klogger/logger.rb +12 -5
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ec65c2728e6aee2870704aeb67e2224f3ffe8b94173f0dafa5e54354f9ec4737
|
4
|
+
data.tar.gz: 51244c5f4ab5fc54d5a9dda4aa14a1051fed37fd718b205c3bd14cf62b7d52ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dab76a4af78b3684b078caf4348712637b952bd4abdc61cf73ba5bc1722feb6754b3dee5eaa4d1a49f5bf22cb86c5a30a5570861822735a64c4c379c56bfe27b
|
7
|
+
data.tar.gz: 0b4fe70463502a574a6e461aec57559bf26e1141ae24d1b129fb2320058fb1acba31c42979564db4e1058c42f4785034fdb586ea05a5e8b5c944c53a4c48ca20
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 Krystal Hosting Ltd.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
![Screenshot](https://share.adam.ac/23/Screen-Shot-2023-03-09-16-00-57.65-DWjwl4M5Gu.png)
|
2
|
+
|
3
|
+
<p align="center">
|
4
|
+
<a href="https://rubygems.org/gems/klogger-logger">
|
5
|
+
<img src="https://img.shields.io/gem/v/klogger-logger?label=RubyGems&logo=rubygems" alt="RubyGems">
|
6
|
+
</a>
|
7
|
+
<a href="https://github.com/krystal/klogger/actions">
|
8
|
+
<img src="https://img.shields.io/github/actions/workflow/status/krystal/klogger/commit.yaml?branch=main&logo=github" alt="Actions Status">
|
9
|
+
</a>
|
10
|
+
<a href="https://github.com/krystal/klogger/blob/main/LICENSE">
|
11
|
+
<img src="https://img.shields.io/github/license/krystal/klogger.svg?style=flat" alt="License Status">
|
12
|
+
</a>
|
13
|
+
</p>
|
14
|
+
|
15
|
+
Klogger is an opinionated logger for Ruby applications to allow consistent and structured logging.
|
16
|
+
|
17
|
+
- Output can be sent to STDOUT or a file. The logger is backed by the standard Ruby `Logger` class.
|
18
|
+
- Ouput can be presented in various formats.
|
19
|
+
- Output can be highlighted with appropriate colours based on severity (optional).
|
20
|
+
- Additional destinations can easily be added for shipping log data to other services.
|
21
|
+
|
22
|
+
## Installation
|
23
|
+
|
24
|
+
Add the gem to your Gemfile.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
gem "klogger-logger", '~> 1.1'
|
28
|
+
```
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
This shows some typical usage of the logger along with example output. The `Klogger::Logger` instance will output to STDOUT by default but can be redirectly.
|
33
|
+
|
34
|
+
### Setting up a logger
|
35
|
+
|
36
|
+
To begin, you need an instance of your logger. Loggers are thread-safe so you can use a single instance across multiple classes and requests. There are a few options that you can control about a logger. These are documented below.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
# The most basic logger includes a name and nothing else. This will log to STDOUT and use
|
40
|
+
# the default formatter without colouring.
|
41
|
+
Klogger.new(name)
|
42
|
+
|
43
|
+
# You can customise where log output goes using the destination argument. You can provide a device
|
44
|
+
# that response to write & close or a path to a file. The same as Ruby's logger class.
|
45
|
+
Klogger.new(name, destination: $stderr)
|
46
|
+
Klogger.new(name, destination: 'log/events.log')
|
47
|
+
Klogger.new(name, destination: StringIO.new)
|
48
|
+
|
49
|
+
# To customise the formatting of the log output, you can provide a formatter.
|
50
|
+
Klogger.new(name, formatter: :json)
|
51
|
+
Klogger.new(name, formatter: :simple)
|
52
|
+
Klogger.new(name, formatter: :go)
|
53
|
+
|
54
|
+
# You can also enable colouring/highlighting.
|
55
|
+
Klogger.new(name, highlight: true)
|
56
|
+
Klogger.new(name, highlight: Rails.env.development?)
|
57
|
+
|
58
|
+
# You can add tags to be included in all log lines for this logger.
|
59
|
+
Klogger.new(name, tags: { app: 'example-app' })
|
60
|
+
```
|
61
|
+
|
62
|
+
### Logging
|
63
|
+
|
64
|
+
When you want to log something, you have the option of 5 severities (`debug`, `info`, `warn`, `error` and `fatal`). For this example, we'll use `info` but it is interchangable with any of the other severities.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
# The most basic way to log is to provide a message.
|
68
|
+
logger.info("Hello world!")
|
69
|
+
|
70
|
+
# To add additional tags to the line, you can do so by passing a hash.
|
71
|
+
logger.info("Sending e-mail", recipient: "adam@example.com", subject: "Hello world!")
|
72
|
+
|
73
|
+
# The message is optional and you can just pass a hash too
|
74
|
+
logger.info(ip_address: "1.2.3.4", method: "POST", path: "/login")
|
75
|
+
|
76
|
+
# Blocks can also be passed to the logger to log the result of that block
|
77
|
+
logger.info { "Hello world!" }
|
78
|
+
logger.info('Result of 1 + 1') { 1 + 1 } # Logs with a message of "Result: 2"
|
79
|
+
```
|
80
|
+
|
81
|
+
### Loggging exceptions
|
82
|
+
|
83
|
+
Exceptions happen and when they do, you want to know about them. Klogger provides a helper method to log exceptions. These will automatically be logged with the `error` severity.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
begin
|
87
|
+
# Do somethign bad
|
88
|
+
rescue => e
|
89
|
+
# Just log the exception
|
90
|
+
logger.exception(e)
|
91
|
+
|
92
|
+
# You can also provide a message
|
93
|
+
logger.exception(e, "Something went wrong")
|
94
|
+
|
95
|
+
# You can also provide a hash of additional tags
|
96
|
+
logger.exception(e, "Something went wrong", tags: { user_id: 123 })
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
### Groups
|
101
|
+
|
102
|
+
Groups allow you to group related log entries together. They do two things:
|
103
|
+
|
104
|
+
1. They allow you to add tags to all logs which are within the group
|
105
|
+
2. They assign a random ID to the group which is included with all logs within the group
|
106
|
+
|
107
|
+
Here's an example of how they work.
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
# In this example, both log entries within the block will be tagged with the `url` tag from the group.
|
111
|
+
logger.group(url: "https://example.com/my-files.zip") do # 92b1b62c
|
112
|
+
logger.info("Download starting")
|
113
|
+
file = download_file('...')
|
114
|
+
logger.info("Download complete", size: file.size)
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
You'll notice in that example the comment `92b1b62c`. This is the group ID for this block. This is a random ID which is generated when the group is created. It is included with all logs within the group thus allowing you to search for that reference to find all logs related to that group. If groups are nested, you'll have multiple IDs. By default, these group IDs are not shown in your output.
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
# If you wish for group IDs to be included in your output, you can enable that in the logger
|
122
|
+
Klogger.new(name, include_group_ids: true)
|
123
|
+
```
|
124
|
+
|
125
|
+
When executed like this groups will only apply to the logger in question. If you want to group messages from different loggers, you can use the `Klogger.group` method where groups and their data will apply to all Klogger loggers.
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
logger1 = Klogger.new(:logger1)
|
129
|
+
logger2 = Klogger.new(:logger2)
|
130
|
+
Klogger.group(ip: '1.2.3.4') do
|
131
|
+
logger1.info('Hello world!') # will be tagged with ip=1.2.3.4
|
132
|
+
Klogger.group(user: 'user_abcdef')
|
133
|
+
logger2.info('Example') # will be tagged with ip=1.2.3.4 and user=user_abcdef
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# If you can't use a block you can manually open and close a group but you'll need to be sure to close it
|
138
|
+
# when you're finished.
|
139
|
+
group_id = Klogger.global_groups.add(ip: '1.2.3.4')
|
140
|
+
# ... do anything that you want - everything will be tagged as appropriate
|
141
|
+
Klogger.global_groups.pop
|
142
|
+
```
|
143
|
+
|
144
|
+
Finally, you can use groups without IDs to simply add tags to all logs within the group using the `tagged` method.
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
logger.tagged(name: 'steve') do
|
148
|
+
logger.info("Download starting")
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
### Silencing
|
153
|
+
|
154
|
+
Sometimes you don't want to log for a little while. You can use the `silence` method to temporarily disable logging.
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
# Calling this will silence the logs until you unsilence again
|
158
|
+
logger.silence!
|
159
|
+
logger.unsilence!
|
160
|
+
|
161
|
+
# Alternative, you can use the block option which will unsilence when complete.
|
162
|
+
logger.silence! do
|
163
|
+
# Logs will be silenced here
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
### Sending log data elsewhere
|
168
|
+
|
169
|
+
In many cases you won't want to keep your log data on a local disk or within STDOUT. You can use this additional option to have data dispatched automatically to other services which you decide upon.
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
# This is just an example class. You can create whatever class you want here and it'll be called
|
173
|
+
# with the call method.
|
174
|
+
class GraylogDestination
|
175
|
+
|
176
|
+
def initialize(host, port)
|
177
|
+
@notifier = GELF::Notifier.new(host, port)
|
178
|
+
end
|
179
|
+
|
180
|
+
def call(logger, payload, group_ids)
|
181
|
+
message = payload.delete(:message)
|
182
|
+
@notifer.notify!(facility: "my-app", short_message: message, group_ids: group_ids, **payload)
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
# Create a logger and add the destination
|
188
|
+
logger = Klogger.new(name)
|
189
|
+
logger.add_destination(GraylogDestination.new('graylog.example.com', 12201))
|
190
|
+
```
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.3.2
|
data/lib/klogger/logger.rb
CHANGED
@@ -2,11 +2,14 @@
|
|
2
2
|
|
3
3
|
require 'logger'
|
4
4
|
require 'securerandom'
|
5
|
+
|
6
|
+
require 'concurrent/atomic/thread_local_var'
|
7
|
+
|
5
8
|
require 'klogger/formatters/json'
|
6
9
|
require 'klogger/formatters/simple'
|
7
10
|
require 'klogger/formatters/go'
|
8
|
-
require 'concurrent/atomic/thread_local_var'
|
9
11
|
require 'klogger/group_set'
|
12
|
+
require 'klogger/version'
|
10
13
|
|
11
14
|
module Klogger
|
12
15
|
class Logger < ::Logger
|
@@ -35,10 +38,14 @@ module Klogger
|
|
35
38
|
end
|
36
39
|
|
37
40
|
def exception(exception, message = nil, **tags)
|
38
|
-
error(
|
39
|
-
|
40
|
-
|
41
|
-
|
41
|
+
error(
|
42
|
+
**{
|
43
|
+
message: message,
|
44
|
+
exception: exception.class.name,
|
45
|
+
exception_message: exception.message,
|
46
|
+
backtrace: exception.backtrace[0, 4].join("\n")
|
47
|
+
}.merge(tags)
|
48
|
+
)
|
42
49
|
end
|
43
50
|
|
44
51
|
LEVELS.each do |level|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: klogger-logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Cooke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|
@@ -71,6 +71,9 @@ executables: []
|
|
71
71
|
extensions: []
|
72
72
|
extra_rdoc_files: []
|
73
73
|
files:
|
74
|
+
- MIT-LICENSE
|
75
|
+
- README.md
|
76
|
+
- VERSION
|
74
77
|
- lib/klogger-logger.rb
|
75
78
|
- lib/klogger.rb
|
76
79
|
- lib/klogger/colors.rb
|