elastic-logger 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/README.md +76 -0
- data/elastic-logger.gemspec +21 -0
- data/lib/elastic-logger.rb +16 -0
- data/lib/elastic-logger/configuration.rb +11 -0
- data/lib/elastic-logger/disk_writer.rb +25 -0
- data/lib/elastic-logger/elk_writer.rb +23 -0
- data/lib/elastic-logger/logger.rb +34 -0
- data/lib/elastic-logger/null_writer.rb +6 -0
- data/lib/elastic-logger/version.rb +3 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 330784ef74bac199f5f7e8f9312eb147561da20f
|
4
|
+
data.tar.gz: 92371c2783a123baa12582c0884975adfe62ca18
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ac919df31c20c1541f51d6b572905828e2b22ea1f36e82d5a3f3ec64a5ce4a2692d51745a7d975b532c8b79241a85211a8b4ecf607ab2c45af1f9156632a2761
|
7
|
+
data.tar.gz: 4960f05cff16ed38c99b5c210e2c41827f1dbdbc061ef1f9402ba8d867dd24589d0c13129e7d8cef2fd332eea701f115cb18822b24eff55256146c884fc680c0
|
data/.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
# Gemfile.lock
|
46
|
+
# .ruby-version
|
47
|
+
# .ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# ElasticLogger
|
2
|
+
|
3
|
+
ElasticLogger is a simple and elastic (;)) solusion for sending JSON logs to elasticsearch but not only there. It works based on writers. Each writer is used to send logs to diffrent destination. Gem is released with two writers one for ElasticSearch and one for standard log files. You can also create your own Writer and use it in config file.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'elastic-logger'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install elastic-logger
|
18
|
+
|
19
|
+
## Usage Examples
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
require 'elastic-logger'
|
23
|
+
|
24
|
+
# Configuration
|
25
|
+
ElasticLogger.configure do |config|
|
26
|
+
config.host = '192.168.60.10:9200'
|
27
|
+
types_file = 'config/elastic_log_config.yml'
|
28
|
+
path = 'log'
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
### Config file
|
33
|
+
With this file we create two logs, one which will log to elastic, and one to standard log file.
|
34
|
+
|
35
|
+
```yml
|
36
|
+
sidekiq_monitor:
|
37
|
+
writer: 'InfaktLogger::ElastickWriter'
|
38
|
+
rotate_type: 'rotate'
|
39
|
+
rotate: 1
|
40
|
+
backup: false
|
41
|
+
api_requests:
|
42
|
+
writer: 'InfaktLogger::DiskWriter'
|
43
|
+
rotate_type: 'rotate'
|
44
|
+
rotate: 3
|
45
|
+
backup: false
|
46
|
+
```
|
47
|
+
### Usage
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
logger = ElasticLogger::Logger.new('sidekiq_monitor')
|
51
|
+
logger.log(foo: :bar)
|
52
|
+
```
|
53
|
+
|
54
|
+
### Writers
|
55
|
+
|
56
|
+
#### ElastickWriter
|
57
|
+
|
58
|
+
Creates daily index named after log name. For sidekiq_monitor it will creates `sidekiq_monitr-%Y.%m.%d` index each day, with `sidekiq_monitor` type and injects there hashes from user input. Next you can go to Kibana and search in it.
|
59
|
+
|
60
|
+
#### DiskWriter
|
61
|
+
|
62
|
+
It is use by us mostly on devels, when we don't want to use elk for logs. Internally it use ruby `Logger`.
|
63
|
+
|
64
|
+
#### Custom Writer
|
65
|
+
|
66
|
+
You can create your own writer. It needs two mtethods:
|
67
|
+
* `initialize` with `name` and `config` named params
|
68
|
+
* `log` with one params with is hash to log.
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create new Pull Request
|
@@ -0,0 +1,21 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'elastic-logger/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "elastic-logger"
|
7
|
+
spec.version = ElasticLogger::VERSION
|
8
|
+
spec.authors = ["Radosław Woźnik", "Infakt DevTeam"]
|
9
|
+
spec.email = ["p@infakt.pl"]
|
10
|
+
|
11
|
+
spec.summary = %q{Elastic Logger, logs to elasticsearch or files}
|
12
|
+
spec.description = %q{Elastic Logger, logs to elasticsearch or files}
|
13
|
+
spec.homepage = "https://github.com/infakt/elastic-logger"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
20
|
+
spec.add_dependency "elasticsearch", "~> 5.0"
|
21
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'elastic_logger/disk_writer'
|
2
|
+
require 'elastic_logger/elk_writer'
|
3
|
+
|
4
|
+
module ElasticLogger
|
5
|
+
class << self
|
6
|
+
attr_writer :configuration
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.configuration
|
10
|
+
@configuration ||= Configuration.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.configure
|
14
|
+
yield(configuration)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ElasticLogger
|
2
|
+
class DiskWriter
|
3
|
+
def initialize(name:, config:)
|
4
|
+
@config = config
|
5
|
+
@name = name
|
6
|
+
end
|
7
|
+
|
8
|
+
def log(hash)
|
9
|
+
logger.info(hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
attr_reader :config, :name
|
14
|
+
|
15
|
+
def logger
|
16
|
+
@logger ||= ::Logger.new(path_with(name), 'monthly').tap { |lgger|
|
17
|
+
lgger.formatter = lambda { |_, _, _, msg| msg.to_h.to_json + "\n" }
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def path_with(name)
|
22
|
+
Pathname.new(config.path).join("#{name}.log")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ElasticLogger
|
2
|
+
class ElkWriter
|
3
|
+
def initialize(name:, config:)
|
4
|
+
@config = config
|
5
|
+
@name = name
|
6
|
+
end
|
7
|
+
|
8
|
+
def log(hash)
|
9
|
+
client.index(index: index, type: name, body: hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
attr_reader :config, :name
|
14
|
+
|
15
|
+
def client
|
16
|
+
@client ||= Elasticsearch::Client.new(host: config.host)
|
17
|
+
end
|
18
|
+
|
19
|
+
def index
|
20
|
+
"#{name}-#{Date.today.strftime('%Y.%m.%d')}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ElasticLogger
|
2
|
+
class Logger
|
3
|
+
def initialize(name)
|
4
|
+
@name = name.to_s
|
5
|
+
end
|
6
|
+
|
7
|
+
def log(hash)
|
8
|
+
writer.log(hash)
|
9
|
+
end
|
10
|
+
alias_method :debug, :log
|
11
|
+
alias_method :info, :log
|
12
|
+
alias_method :warn, :log
|
13
|
+
|
14
|
+
private
|
15
|
+
attr_reader :name
|
16
|
+
|
17
|
+
def writer
|
18
|
+
writer = logs.fetch(name, default).fetch("writer")
|
19
|
+
Object.const_get(writer).new(name: name, config: config)
|
20
|
+
end
|
21
|
+
|
22
|
+
def config
|
23
|
+
@config ||= ElasticLogger.configuration
|
24
|
+
end
|
25
|
+
|
26
|
+
def logs
|
27
|
+
@@logs ||= YAML.load_file(config.types_file)
|
28
|
+
end
|
29
|
+
|
30
|
+
def default
|
31
|
+
{ "writer" => "ElasticLogger::NullLogger" }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: elastic-logger
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Radosław Woźnik
|
8
|
+
- Infakt DevTeam
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-09-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.11'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.11'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: elasticsearch
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '5.0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '5.0'
|
42
|
+
description: Elastic Logger, logs to elasticsearch or files
|
43
|
+
email:
|
44
|
+
- p@infakt.pl
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- README.md
|
51
|
+
- elastic-logger.gemspec
|
52
|
+
- lib/elastic-logger.rb
|
53
|
+
- lib/elastic-logger/configuration.rb
|
54
|
+
- lib/elastic-logger/disk_writer.rb
|
55
|
+
- lib/elastic-logger/elk_writer.rb
|
56
|
+
- lib/elastic-logger/logger.rb
|
57
|
+
- lib/elastic-logger/null_writer.rb
|
58
|
+
- lib/elastic-logger/version.rb
|
59
|
+
homepage: https://github.com/infakt/elastic-logger
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.5.2
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: Elastic Logger, logs to elasticsearch or files
|
83
|
+
test_files: []
|