elklogger 0.0.6

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ae7b440b0b48762f5f4f54993167364e1617a7a
4
+ data.tar.gz: aa2992fcc13ce54c146e816504774ffc36bf70e0
5
+ SHA512:
6
+ metadata.gz: 85c66f50d640c4349aa5d766dbe08198ff53d59bc892728dd79d09a9b58cb7cdc927232a89c96af6a6b2d232c78921c93ea3e0f7956c2d637fa7f7ccaa26ad5e
7
+ data.tar.gz: 2f205bbd1ce95f4bb2f8c4967b88a644a4111a9692505d7c242194ce1ec54996ec9bf11eabc6eb993aafa71ecab11763aff19e0263c83542988ed80a300056fa
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ before_install: gem install bundler -v 1.11.2
data/CHANGELOG ADDED
@@ -0,0 +1,9 @@
1
+ VERSION 0.0.5
2
+ * Use logfile name as thread name for Ruby projects.
3
+ * Automatically generate logstash input file path in *.conf.
4
+ VERSION 0.0.4
5
+ * Fix redefine ElkLogger class.
6
+ VERSION 0.0.3
7
+ * Don't add +try+ method when it's been defined (such as in a RAILS application).
8
+ * Change class name to +ElkLogger+ .
9
+ * Add config generator, now you can use +elklogger config:install+ to generate config file.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in elklogger.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Hayden Wei
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # ElkLogger
2
+
3
+ A formatted logger for ELK-stack.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'elklogger'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install elklogger
20
+
21
+ ## Usage
22
+
23
+ First, execute the following command to generate config file:
24
+
25
+ ```shel
26
+ elklogger config:install
27
+ ```
28
+
29
+ then you can use it like this in your ruby source files:
30
+
31
+ ```ruby
32
+ logger = ElkLogger.new('/tmp/filename.log.elk') # Create log file, filename MUST be ending with '.elk'
33
+ logger.info 'hello logger!' # Use 'info' level to log information
34
+ logger.close
35
+ ```
36
+
37
+ **NOTE:** logfile's postfix MUST BE `.elk`, this is important, because only `.elk` files will be supported.
38
+
39
+ And the supported log levels are (ordered by level ASC):
40
+
41
+ - debug
42
+ - info
43
+ - warn
44
+ - error
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
51
+
52
+ ## Contributing
53
+
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gnodiah/elklogger.
55
+
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
60
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "elklogger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/elklogger ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path('../../lib', __FILE__)
4
+ require "#{lib}/elklogger/generator/install_config"
5
+ require "#{lib}/elklogger/version"
6
+
7
+ if ARGV.size != 1
8
+ puts "Wrong number of arguments (#{ARGV.size} for 1), type 'elklogger -h' for more help."
9
+ elsif ARGV.first != 'config:install'
10
+ if ARGV.first == '-h'
11
+ puts <<-EOF
12
+ Usage:
13
+ elklogger config:install, Add elklogger config file
14
+ elklogger -v, Show ElkLogger version
15
+ elklogger -h, Show help information
16
+ EOF
17
+ elsif ARGV.first == '-v'
18
+ puts ElkLogger::VERSION
19
+ else
20
+ puts "Wrong argument: #{ARGV.first}, type 'elklogger -h' for more help."
21
+ end
22
+ else
23
+ Elklogger::Generator.install
24
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/elklogger.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elklogger/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "elklogger"
8
+ spec.version = ElkLogger::VERSION
9
+ spec.authors = ["Hayden Wei"]
10
+ spec.email = ["haidongrun@gmail.com"]
11
+
12
+ spec.summary = %q{Specific formatted logger for ELK-stack.}
13
+ spec.description = %q{Write formatted log infos to log file, used by ELK-stack.}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "bin"
27
+ spec.executables = %w(elklogger) # spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ spec.required_ruby_version = '>= 2.0.0'
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.11"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ end
@@ -0,0 +1,15 @@
1
+ class Object
2
+ def try(*a, &b)
3
+ if a.empty? && block_given?
4
+ yield self
5
+ else
6
+ __send__(*a, &b)
7
+ end
8
+ end
9
+ end
10
+
11
+ class NilClass
12
+ def try(*args)
13
+ nil
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ require 'fileutils'
2
+
3
+ # Install ElkLogger's config file.
4
+ module Elklogger
5
+ class Generator
6
+ def self.install
7
+ src_root = File.dirname(__FILE__)
8
+ dst_root = FileUtils.pwd #defined?(Rails) ? Rails.root : src_root
9
+ src = "#{src_root}/template/custom_log.conf"
10
+ dst = "#{dst_root}/config/elklogger"
11
+
12
+ FileUtils.mkdir_p(dst) unless File.directory?(dst)
13
+ dst += '/custom_log.conf'
14
+ FileUtils.copy_file(src, dst)
15
+
16
+ # Automatically generate logstash input files path, it will be
17
+ # "#{the_current_dir}/log/*.log.elk"
18
+ contents = File.read(dst)
19
+ contents.gsub!('your_ruby_project_absolute_path', dst_root)
20
+ File.open(dst, 'w') { |f| f.write contents }
21
+
22
+ # TODO Automatically generate kafka servers
23
+
24
+ puts "Created config file: #{File.expand_path(dst)}"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ input {
2
+ file {
3
+ path => "your_ruby_project_absolute_path/log/*.log.elk"
4
+ # start_position => "beginning"
5
+ }
6
+ }
7
+
8
+ filter {
9
+ json {
10
+ source => "message"
11
+ remove_field => ["@version", "@timestamp", "path", "host", "message"]
12
+ }
13
+
14
+ }
15
+
16
+ output {
17
+ kafka {
18
+ bootstrap_servers => "your_kafka_bootstrap_servers"
19
+ topic_id => "EAGLEYE_LOG_CHANNEL"
20
+ }
21
+
22
+ # stdout {
23
+ # codec => rubydebug
24
+ # }
25
+ }
@@ -0,0 +1,6 @@
1
+ require 'logger'
2
+
3
+ class ElkLogger < Logger
4
+ SUPER_VERSION = VERSION # keep superclass (Logger) version
5
+ VERSION = "0.0.6"
6
+ end
data/lib/elklogger.rb ADDED
@@ -0,0 +1,112 @@
1
+ require "elklogger/version"
2
+ require "elklogger/core_ext/object/try" unless defined?(try)
3
+ require 'date'
4
+ require 'json/ext'
5
+ require 'socket'
6
+ require 'pathname'
7
+
8
+ class Logger
9
+
10
+ class LogDevice
11
+ private
12
+
13
+ def add_log_header(file); "\n"; end
14
+ end
15
+ end
16
+
17
+ class ElkLogger
18
+
19
+ module LoggerInfo
20
+ def ruby_pid; $$.to_s; end
21
+
22
+ # In fact, we should record thread's name here. But actually
23
+ # we don't care about thread name in Ruby, we even more care
24
+ # about logfile's name. So, here we use logfile name instead
25
+ # of thread name.
26
+ # If you want to record thread name, just uncomment the lines
27
+ # in the following method.
28
+ def thread_name
29
+ # Thread.current.inspect.match(/Thread:\w+/).to_s
30
+ end
31
+
32
+ # def method_name; __callee__; end
33
+ # def class_name; end
34
+
35
+ def ipv4_address
36
+ # First IPv4 address
37
+ # ipv4_addr = Socket.ip_address_list.detect { |intf| intf.ipv4_private? }
38
+
39
+ ipv4_addr = Socket.ip_address_list.detect { |intf|
40
+ intf.ipv4? and !intf.ipv4_loopback? and !intf.ipv4_multicast?
41
+ # and !intf.ipv4_private?
42
+ }
43
+ ipv4_addr.ip_address unless ipv4_addr.nil?
44
+ end
45
+
46
+ def appname
47
+ app_name = defined?(Settings) && Settings.try(:elklogger).try(:appname)
48
+ app_name ? app_name.strip : 'elklogger-not-specified'
49
+ end
50
+
51
+ # TODO How to number each line of log files?
52
+ def counter_number
53
+ 0
54
+ end
55
+ end
56
+ include LoggerInfo
57
+
58
+ undef <<
59
+ undef fatal?
60
+ undef fatal
61
+ undef unknown
62
+ undef datetime_format=
63
+ undef datetime_format
64
+
65
+ attr_reader :calling_mname, :calling_cname, :filename
66
+
67
+ def initialize(logdev, shift_age = 0, shift_size = 1048576)
68
+ @calling_mname = nil # calling method name
69
+ @calling_cname = nil # calling class name
70
+ @filename = logdev
71
+
72
+ super(logdev, shift_age, shift_size)
73
+ end
74
+
75
+ def format_message(severity, datetime, progname, msg)
76
+ {
77
+ :body => msg.to_s,
78
+ :head => {
79
+ :app => appname,
80
+ :level => severity,
81
+ :counter => counter_number,
82
+ :tname => thread_name || filename.to_s.split('/').last.to_s.gsub(/\.log\.elk$/, ''),
83
+ :pid => ruby_pid,
84
+ :mname => calling_mname,
85
+ :cname => calling_cname,
86
+ :ip => ipv4_address.to_s
87
+ },
88
+ :timestamp => datetime.to_datetime.strftime("%Q").to_i
89
+ }.to_json + "\n"
90
+ end
91
+
92
+ private
93
+
94
+ SEV_LABEL = %w(DEBUG INFO WARN ERROR ANY)
95
+
96
+ def add(severity, message = nil, progname = nil, &block)
97
+ caller_info = caller_locations(2, 1).first
98
+ @calling_mname = caller_info.try(:label).to_s
99
+
100
+ @calling_cname = caller_info.try(:path).to_s
101
+ if defined?(Rails) && !@calling_cname.empty?
102
+ @calling_cname = Pathname.new(@calling_cname).relative_path_from(Rails.root)
103
+ end
104
+ @calling_cname = @calling_cname.to_s + ":#{caller_info.try(:lineno).to_s}"
105
+
106
+ super(severity, message, progname, &block)
107
+ end
108
+
109
+ class Configuration
110
+ end
111
+
112
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elklogger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.6
5
+ platform: ruby
6
+ authors:
7
+ - Hayden Wei
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Write formatted log infos to log file, used by ELK-stack.
56
+ email:
57
+ - haidongrun@gmail.com
58
+ executables:
59
+ - elklogger
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CHANGELOG
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/elklogger
73
+ - bin/setup
74
+ - elklogger.gemspec
75
+ - lib/elklogger.rb
76
+ - lib/elklogger/core_ext/object/try.rb
77
+ - lib/elklogger/generator/install_config.rb
78
+ - lib/elklogger/generator/template/custom_log.conf
79
+ - lib/elklogger/version.rb
80
+ homepage: ''
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ allowed_push_host: https://rubygems.org
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: 2.0.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.8
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Specific formatted logger for ELK-stack.
105
+ test_files: []
106
+ has_rdoc: