betterlog 0.13.1 → 0.15.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 29fe6dd6fca5b0294e9f55388615d6d8b58885a2f1898682525d19f239bd4552
4
- data.tar.gz: f5495f46f7a6473edbe7a1e4973f9ac021213eb121dd149f5b50d23d191ab9dc
3
+ metadata.gz: 27e6af480e7ec65272867c76176a644ce2c925e853aff060074840f17ad5d714
4
+ data.tar.gz: 0531306c3a7b421691b67e78be5d7e960c91cb421e4e2db22657cdbe2cc248e0
5
5
  SHA512:
6
- metadata.gz: 1345db77124853df14e65a3bfb5680b7936de5711cc0f21449d172822e8f44981a500b03cc29a072781039e4692cfc544e63387d817fa1eb65c4ea55995d83c3
7
- data.tar.gz: f63ed3284d529d6df4f76f3e885d1c52d6d7dbba024ee08c61fcce8e79e0b8f3e04c1a4d15c7c7de7f017568a3ffea0c0cdd9c4da7f649b38172317abf735a09
6
+ metadata.gz: '096483149f340b0cb1f8fc5253ad033e7ce7a4dc4abdd2c07c78beb464a8cd95538b956c4f82b45133d2287cfb767c754fdd1fd8079de780534ed9def9532267'
7
+ data.tar.gz: 610301cb7fc4b028816d78f5ce5f8330b5c04e291ff5099035af49ab3de85def2048455276767b0253f60bebc9d7dd1eb9fb3c3902414ae60e79bf4057ac054b
@@ -0,0 +1,63 @@
1
+ version: v1.0
2
+ name: Betterlog pipeline
3
+ agent:
4
+ machine:
5
+ type: e1-standard-2
6
+ os_image: ubuntu1804
7
+
8
+ blocks:
9
+ - name: Caching
10
+ task:
11
+ prologue:
12
+ commands:
13
+ - checkout
14
+ jobs:
15
+ - name: cache bundle
16
+ commands:
17
+ - sem-version ruby 2.7.2
18
+ - cache restore gems-$SEMAPHORE_GIT_BRANCH,gems-master
19
+ - bundle config set path 'vendor/bundle'
20
+ - bundle config jobs $(getconf _NPROCESSORS_ONLN)
21
+ - bundle install
22
+ - cache store gems-$SEMAPHORE_GIT_BRANCH vendor/bundle
23
+
24
+ - name: "Unit tests"
25
+ task:
26
+ env_vars:
27
+ - name: RAILS_ENV
28
+ value: test
29
+ prologue:
30
+ commands:
31
+ - checkout
32
+
33
+ # Setup ruby
34
+ - sem-version ruby 2.7.2
35
+
36
+ # Setup gems
37
+ - cache restore gems-$SEMAPHORE_GIT_BRANCH,gems-master
38
+ - bundle config set path 'vendor/bundle'
39
+ - bundle config jobs $(getconf _NPROCESSORS_ONLN)
40
+ - bundle install
41
+
42
+ jobs:
43
+ - name: RSpec Unit Tests
44
+ commands:
45
+ - bundle exec rake
46
+
47
+ - name: Building Docker Image
48
+ task:
49
+
50
+ secrets:
51
+ - name: GCP
52
+
53
+ prologue:
54
+ commands:
55
+ - gcloud auth activate-service-account --key-file=.secrets/gcp.json
56
+ - gcloud auth configure-docker -q eu.gcr.io
57
+ - checkout
58
+
59
+ jobs:
60
+ - name: Building and pushing
61
+ commands:
62
+ - make build
63
+ - test "$SEMAPHORE_GIT_BRANCH" = "master" && make push-latest
data/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM alpine:3.12.0 AS builder
1
+ FROM alpine:3.12.3 AS builder
2
2
 
3
3
  # Update/Upgrade/Add packages for building
4
4
 
@@ -16,9 +16,9 @@ RUN make clobber
16
16
 
17
17
  RUN go get -u github.com/betterplace/go-init
18
18
 
19
- RUN make fetch all
19
+ RUN make setup all
20
20
 
21
- FROM alpine:3.12.0 AS runner
21
+ FROM alpine:3.12.3 AS runner
22
22
 
23
23
  # Update/Upgrade/Add packages
24
24
 
data/Makefile CHANGED
@@ -18,7 +18,7 @@ betterlog-server: cmd/betterlog-server/main.go betterlog/*.go
18
18
  local: betterlog-server
19
19
  REDIS_URL=$(REDIS_URL) ./betterlog-server
20
20
 
21
- fetch: fake-package
21
+ setup: fake-package
22
22
  go mod download
23
23
 
24
24
  fake-package:
data/Rakefile CHANGED
@@ -30,6 +30,7 @@ GemHadar do
30
30
  development_dependency 'rake'
31
31
  development_dependency 'rspec'
32
32
  development_dependency 'simplecov'
33
+ development_dependency 'mock_redis'
33
34
  end
34
35
 
35
36
  task :default => :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.13.1
1
+ 0.15.2
data/betterlog.gemspec CHANGED
@@ -1,22 +1,22 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: betterlog 0.13.1 ruby lib
2
+ # stub: betterlog 0.15.2 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "betterlog".freeze
6
- s.version = "0.13.1"
6
+ s.version = "0.15.2"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
10
10
  s.authors = ["betterplace Developers".freeze]
11
- s.date = "2020-07-20"
11
+ s.date = "2021-02-03"
12
12
  s.description = "This library provides structure json logging for our rails projects".freeze
13
13
  s.email = "developers@betterplace.org".freeze
14
14
  s.executables = ["betterlog".freeze, "betterlog_pusher".freeze, "betterlog_sink".freeze]
15
15
  s.extra_rdoc_files = ["README.md".freeze, "lib/betterlog.rb".freeze, "lib/betterlog/global_metadata.rb".freeze, "lib/betterlog/log.rb".freeze, "lib/betterlog/log/event.rb".freeze, "lib/betterlog/log/event_formatter.rb".freeze, "lib/betterlog/log/severity.rb".freeze, "lib/betterlog/log_event_formatter.rb".freeze, "lib/betterlog/logger.rb".freeze, "lib/betterlog/notifiers.rb".freeze, "lib/betterlog/railtie.rb".freeze, "lib/betterlog/version.rb".freeze]
16
- s.files = [".gitignore".freeze, ".travis.yml".freeze, "Dockerfile".freeze, "Gemfile".freeze, "LICENSE".freeze, "Makefile".freeze, "README.md".freeze, "Rakefile".freeze, "TODO.md".freeze, "VERSION".freeze, "betterlog.gemspec".freeze, "betterlog/config.go".freeze, "betterlog/healthz.go".freeze, "betterlog/redis_cert_cache.go".freeze, "bin/betterlog".freeze, "bin/betterlog_pusher".freeze, "bin/betterlog_sink".freeze, "cloudbuild.yaml".freeze, "cmd/betterlog-server/LICENSE".freeze, "cmd/betterlog-server/main.go".freeze, "config/log.yml".freeze, "go.mod".freeze, "go.sum".freeze, "lib/betterlog.rb".freeze, "lib/betterlog/global_metadata.rb".freeze, "lib/betterlog/log.rb".freeze, "lib/betterlog/log/event.rb".freeze, "lib/betterlog/log/event_formatter.rb".freeze, "lib/betterlog/log/severity.rb".freeze, "lib/betterlog/log_event_formatter.rb".freeze, "lib/betterlog/logger.rb".freeze, "lib/betterlog/notifiers.rb".freeze, "lib/betterlog/railtie.rb".freeze, "lib/betterlog/version.rb".freeze, "spec/betterlog/global_metadata_spec.rb".freeze, "spec/betterlog/log/event_spec.rb".freeze, "spec/betterlog/log/severity_spec.rb".freeze, "spec/betterlog/log_spec.rb".freeze, "spec/betterlog/logger_spec.rb".freeze, "spec/spec_helper.rb".freeze]
16
+ s.files = [".gitignore".freeze, ".semaphore/semaphore.yml".freeze, "Dockerfile".freeze, "Gemfile".freeze, "LICENSE".freeze, "Makefile".freeze, "README.md".freeze, "Rakefile".freeze, "TODO.md".freeze, "VERSION".freeze, "betterlog.gemspec".freeze, "betterlog/config.go".freeze, "betterlog/healthz.go".freeze, "betterlog/redis_cert_cache.go".freeze, "bin/betterlog".freeze, "bin/betterlog_pusher".freeze, "bin/betterlog_sink".freeze, "cloudbuild.yaml".freeze, "cmd/betterlog-server/LICENSE".freeze, "cmd/betterlog-server/main.go".freeze, "config/log.yml".freeze, "go.mod".freeze, "go.sum".freeze, "lib/betterlog.rb".freeze, "lib/betterlog/global_metadata.rb".freeze, "lib/betterlog/log.rb".freeze, "lib/betterlog/log/event.rb".freeze, "lib/betterlog/log/event_formatter.rb".freeze, "lib/betterlog/log/severity.rb".freeze, "lib/betterlog/log_event_formatter.rb".freeze, "lib/betterlog/logger.rb".freeze, "lib/betterlog/notifiers.rb".freeze, "lib/betterlog/railtie.rb".freeze, "lib/betterlog/version.rb".freeze, "spec/betterlog/global_metadata_spec.rb".freeze, "spec/betterlog/log/event_spec.rb".freeze, "spec/betterlog/log/severity_spec.rb".freeze, "spec/betterlog/log_spec.rb".freeze, "spec/betterlog/logger_spec.rb".freeze, "spec/spec_helper.rb".freeze]
17
17
  s.homepage = "http://github.com/betterplace/betterlog".freeze
18
18
  s.rdoc_options = ["--title".freeze, "Betterlog".freeze, "--main".freeze, "README.md".freeze]
19
- s.rubygems_version = "3.1.2".freeze
19
+ s.rubygems_version = "3.2.5".freeze
20
20
  s.summary = "Structured logging support for bp".freeze
21
21
  s.test_files = ["spec/betterlog/global_metadata_spec.rb".freeze, "spec/betterlog/log/event_spec.rb".freeze, "spec/betterlog/log/severity_spec.rb".freeze, "spec/betterlog/log_spec.rb".freeze, "spec/betterlog/logger_spec.rb".freeze, "spec/spec_helper.rb".freeze]
22
22
 
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
29
29
  s.add_development_dependency(%q<rake>.freeze, [">= 0"])
30
30
  s.add_development_dependency(%q<rspec>.freeze, [">= 0"])
31
31
  s.add_development_dependency(%q<simplecov>.freeze, [">= 0"])
32
+ s.add_development_dependency(%q<mock_redis>.freeze, [">= 0"])
32
33
  s.add_runtime_dependency(%q<tins>.freeze, ["~> 1.3", ">= 1.22.0"])
33
34
  s.add_runtime_dependency(%q<complex_config>.freeze, [">= 0"])
34
35
  s.add_runtime_dependency(%q<file-tail>.freeze, ["~> 1.0"])
@@ -42,6 +43,7 @@ Gem::Specification.new do |s|
42
43
  s.add_dependency(%q<rake>.freeze, [">= 0"])
43
44
  s.add_dependency(%q<rspec>.freeze, [">= 0"])
44
45
  s.add_dependency(%q<simplecov>.freeze, [">= 0"])
46
+ s.add_dependency(%q<mock_redis>.freeze, [">= 0"])
45
47
  s.add_dependency(%q<tins>.freeze, ["~> 1.3", ">= 1.22.0"])
46
48
  s.add_dependency(%q<complex_config>.freeze, [">= 0"])
47
49
  s.add_dependency(%q<file-tail>.freeze, ["~> 1.0"])
data/bin/betterlog CHANGED
@@ -96,10 +96,10 @@ module Betterlog
96
96
  return true unless @opts[?s]
97
97
  @opts[?s].all? do |param|
98
98
  case param
99
- when /:\?\z/
100
- event[$`].present?
101
- when /:([^:]+)\z/
102
- event[$`].full?(:include?, $1)
99
+ when /\A([^:]+):([^=]+)=(.+)\z/
100
+ event[$1]&.[]($2.to_sym)&.include?($3)
101
+ when /\A([^=]+)=(.+)\z/
102
+ event[$1]&.include?($2)
103
103
  when String
104
104
  event.to_json.include?(@opts[?s])
105
105
  else
data/lib/betterlog.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  require 'tins/xt'
2
2
  require 'json'
3
- require 'logger'
4
- require 'time'
5
- require 'term/ansicolor'
3
+ require 'complex_config'
6
4
 
7
5
  module Betterlog
8
6
  end
@@ -13,7 +11,6 @@ require 'betterlog/global_metadata'
13
11
  require 'betterlog/logger'
14
12
 
15
13
  if defined? Rails
16
- require 'betterlog/log_event_formatter'
17
14
  require 'betterlog/railtie'
18
15
  end
19
16
 
data/lib/betterlog/log.rb CHANGED
@@ -7,12 +7,16 @@ require 'betterlog/log/severity'
7
7
  module Betterlog
8
8
  class Log
9
9
  include Tins::SexySingleton
10
+ extend ComplexConfig::Provider::Shortcuts
10
11
 
11
12
  class_attr_accessor :default_logger
12
13
  self.default_logger = Logger.new(STDERR)
14
+ if level = cc.log?&.level?
15
+ default_logger.level = level
16
+ end
13
17
 
14
18
  def logger
15
- defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : ::Logger.new(STDERR)
19
+ defined?(Rails) && Rails.respond_to?(:logger) ? Rails.logger : self.class.default_logger
16
20
  end
17
21
 
18
22
  # Logs a message on severity info.
@@ -134,7 +138,7 @@ module Betterlog
134
138
  end
135
139
  event[:emitter] = self.class.name.downcase
136
140
  notify(event)
137
- logger.send(event.severity.to_sym, event.to_json)
141
+ logger.send(event.severity.to_sym, JSON.generate(event))
138
142
  self
139
143
  ensure
140
144
  GlobalMetadata.data.clear
@@ -75,7 +75,15 @@ module Betterlog
75
75
  end
76
76
 
77
77
  def to_json(*a)
78
- as_json.to_json(*a)
78
+ JSON.generate(as_json)
79
+ rescue
80
+ # Sometimes rails logging messages contain invalid utf-8 characters
81
+ # generating various standard errors. Let's fallback to a barebones
82
+ # event with just a cleaned up message for these cases.
83
+ JSON.generate({
84
+ severity: @data[:severity],
85
+ message: @data.fetch(:message, '').encode('utf-8', invalid: :replace, undef: :replace, replace: ''),
86
+ })
79
87
  end
80
88
 
81
89
  def format(*args)
@@ -1,7 +1,11 @@
1
+ require 'time'
2
+ require 'term/ansicolor'
3
+
1
4
  module Betterlog
2
5
  class Log
3
6
  class EventFormatter
4
7
  include Term::ANSIColor
8
+ include ComplexConfig::Provider::Shortcuts
5
9
 
6
10
  def initialize(event)
7
11
  @event = event
@@ -14,7 +18,7 @@ module Betterlog
14
18
  when :format
15
19
  format_pattern(format: format)
16
20
  else
17
- @event.to_json
21
+ JSON.generate(@event)
18
22
  end
19
23
  ensure
20
24
  Term::ANSIColor.coloring = old_coloring
@@ -1,3 +1,5 @@
1
+ require 'logger'
2
+
1
3
  module Betterlog
2
4
  class Log
3
5
  class Severity
@@ -1,6 +1,7 @@
1
1
  module Betterlog
2
2
  class LogEventFormatter < ActiveSupport::Logger::Formatter
3
3
  include ActiveSupport::TaggedLogging::Formatter
4
+ include ComplexConfig::Provider::Shortcuts
4
5
 
5
6
  def emitter
6
7
  'legacy'
@@ -35,7 +36,7 @@ module Betterlog
35
36
  event[:location] = [ l.absolute_path, l.lineno ] * ?:
36
37
  end
37
38
  program and event[:program] = program
38
- message = event.to_json
39
+ message = JSON.generate(event)
39
40
  end
40
41
  end
41
42
  rescue => e
@@ -1,10 +1,20 @@
1
- require 'redis'
1
+ begin
2
+ require 'redis' unless defined?(Redis)
3
+ rescue LoadError
4
+ end
5
+ require 'logger'
2
6
 
3
7
  module Betterlog
4
8
  class Logger < ::Logger
9
+ include ComplexConfig::Provider::Shortcuts
10
+
5
11
  def initialize(redis, shift_age = 0, shift_size = 1048576, name: nil, buffer_size: nil, **opts)
6
12
  @redis = redis
7
13
  @fallback = ::Logger.new(STDERR)
14
+ if level = cc.log.level?
15
+ self.level = level
16
+ @fallback.level = level
17
+ end
8
18
  @name = name || self.class.name
9
19
  @buffer_size = determine_buffer_size(buffer_size)
10
20
  super(nil, shift_age, shift_size, **opts)
@@ -92,7 +102,9 @@ module Betterlog
92
102
  s = 0
93
103
  e = @redis.strlen(name_tmp) - 1
94
104
  until s > e
95
- y.yield @redis.getrange(name_tmp, s, s + chunk_size - 1)
105
+ range = @redis.getrange(name_tmp, s, s + chunk_size - 1)
106
+ range.force_encoding 'ASCII-8BIT'
107
+ y.yield range
96
108
  s += chunk_size
97
109
  end
98
110
 
@@ -115,9 +127,10 @@ module Betterlog
115
127
  chunk_size > 0 or raise ArgumentError, 'chunk_size > 0 required'
116
128
  Enumerator.new do |y|
117
129
  buffer = ''
130
+ buffer.encode! 'ASCII-8BIT'
118
131
  each_chunk(chunk_size: chunk_size) do |chunk|
119
132
  buffer << chunk
120
- buffer.gsub!(/\A(.*?#$/)/) do |line|
133
+ buffer.gsub!(/\A(.*?#$/)/n) do |line|
121
134
  y.yield(line)
122
135
  ''
123
136
  end
@@ -1,6 +1,7 @@
1
1
  module Betterlog
2
2
  class Railtie < Rails::Railtie
3
3
  initializer "betterlog_railtie.configure_rails_initialization" do
4
+ require 'betterlog/log_event_formatter'
4
5
  Rails.logger.formatter = Betterlog::LogEventFormatter.new
5
6
  end
6
7
  end
@@ -1,6 +1,6 @@
1
1
  module Betterlog
2
2
  # Betterlog version
3
- VERSION = '0.13.1'
3
+ VERSION = '0.15.2'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -26,6 +26,17 @@ describe Betterlog::Log do
26
26
  end
27
27
  end
28
28
 
29
+ describe 'Log::Event.to_json' do
30
+ it 'can be called' do
31
+ expect(event.to_json).to be_present
32
+ end
33
+
34
+ it 'can handle invalid UTF-8 characters' do
35
+ event = Log::Event.ify("foo\xCEbar")
36
+ expect(event.to_json).to eq(JSON(severity: "DEBUG", message: 'foobar'))
37
+ end
38
+ end
39
+
29
40
  describe '.parse' do
30
41
  it 'can parse an event as a JSON document' do
31
42
  expect(Log::Event.parse(event.to_json)).to eq event
data/spec/spec_helper.rb CHANGED
@@ -9,5 +9,11 @@ begin
9
9
  require 'byebug'
10
10
  rescue LoadError
11
11
  end
12
+ require 'mock_redis'
13
+ class MockRedis
14
+ class BaseConnectionError < StandardError; end
15
+ class CannotConnectError < BaseConnectionError; end
16
+ end
17
+ Redis=MockRedis
12
18
  require 'betterlog'
13
19
  Betterlog::Log.default_logger = Logger.new(nil)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: betterlog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.1
4
+ version: 0.15.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - betterplace Developers
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-20 00:00:00.000000000 Z
11
+ date: 2021-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_hadar
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mock_redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: tins
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -206,7 +220,7 @@ extra_rdoc_files:
206
220
  - lib/betterlog/version.rb
207
221
  files:
208
222
  - ".gitignore"
209
- - ".travis.yml"
223
+ - ".semaphore/semaphore.yml"
210
224
  - Dockerfile
211
225
  - Gemfile
212
226
  - LICENSE
@@ -267,7 +281,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
267
281
  - !ruby/object:Gem::Version
268
282
  version: '0'
269
283
  requirements: []
270
- rubygems_version: 3.1.2
284
+ rubygems_version: 3.2.5
271
285
  signing_key:
272
286
  specification_version: 4
273
287
  summary: Structured logging support for bp
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- rvm:
2
- - 2.6
3
- - 2.7
4
- sudo: false
5
- services:
6
- - redis-server