Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit each log line length #47

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ jobs:
run: bundle exec ci-helper CheckSpecSuffixes --extra-paths spec/*.rb --ignored-paths spec/*_helper.rb
- name: Run specs
run: bundle exec ci-helper RunSpecs
- name: Audit
run: bundle exec ci-helper BundlerAudit
- name: Documentation coverage
run: bundle exec rake doc:coverage
- name: Coveralls
Expand Down
9 changes: 7 additions & 2 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Changelog

## 1.11.0

* Add `max_log_length` to limit each log line length in order to prevent memory leaks in case of very long logs

## 1.10.0

* Add an ability to insert Lamian's middleware inside the rails initialization process manually (`Lamian.config.middleware_autoset = true/false`)
Expand All @@ -14,7 +20,7 @@

## 1.3.0

* Add support for the (new sentry gem)[https://github.com/getsentry/sentry-ruby].
* Add support for the [new sentry gem](https://github.com/getsentry/sentry-ruby).

## 1.2.0

Expand All @@ -33,7 +39,6 @@ which ruins concept of single entry point :(. Also tied it to lamian instance

* `8136689` fixed crashes when dump used outside lamian context


## 0.3.2

* `e57e6cec` Changed rails dependency from `~> 4.2` to `>= 4.2`
Expand Down
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ source "https://rubygems.org"
# Specify your gem's dependencies in lamian.gemspec
gemspec

gem "bundler-audit"
gem "ci-helper"
gem "launchy"
gem "pry"
Expand Down
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ GEM
base64 (0.2.0)
bigdecimal (3.1.8)
builder (3.3.0)
bundler-audit (0.9.1)
bundler (>= 1.2.0, < 3)
thor (~> 1.0)
childprocess (5.1.0)
logger (~> 1.5)
ci-helper (0.6.0)
Expand Down Expand Up @@ -312,7 +309,6 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
bundler-audit
ci-helper
lamian!
launchy
Expand Down
2 changes: 2 additions & 0 deletions lib/lamian/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ module Lamian
Config = Struct.new(
:formatter,
:max_log_lines,
:max_log_length,
:raven_log_size_limit,
:middleware_autoset,
) do
def initialize
self.formatter = ::Logger::Formatter.new
self.max_log_lines = 5000
self.max_log_length = 10_000
self.raven_log_size_limit = 500_000
self.middleware_autoset = true
end
Expand Down
24 changes: 18 additions & 6 deletions lib/lamian/log_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

module Lamian
class LogDevice # :nodoc:
def initialize(size = Lamian.config.max_log_lines)
self.size = size
def initialize(max_log_lines: Lamian.config.max_log_lines,
max_log_length: Lamian.config.max_log_length)
self.max_log_lines = max_log_lines
self.max_log_length = max_log_length
self.lines = []
end

def write(string) # :nodoc:
lines << string
lines.shift if lines.size > size
def write(msg) # :nodoc:
lines << truncate(msg)
lines.shift if lines.size > max_log_lines
true
end

Expand All @@ -19,6 +21,16 @@ def string # :nodoc:

private

attr_accessor :size, :lines
attr_accessor :lines, :max_log_lines, :max_log_length

def truncate(msg)
return msg unless msg.size > max_log_length

suffix = +"..."
suffix << "\n" if msg.end_with?("\n")
msg = msg[0, max_log_length - suffix.size]

"#{msg}#{suffix}"
end
end
end
22 changes: 18 additions & 4 deletions spec/lamian/log_device_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
# frozen_string_literal: true

describe Lamian::LogDevice do
subject(:logdev) { described_class.new(5) }
subject(:logdev) { described_class.new(max_log_lines: 5, max_log_length: 20) }

it "saves log" do
logdev.write("Hello ")
logdev.write("world!")
expect(logdev.string).to eq("Hello world!")
logdev.write("Hello\n")
logdev.write("world!\n")
expect(logdev.string).to eq("Hello\nworld!\n")
end

it "only stores 5 latest log lines" do
8.times { |x| logdev.write(x + 1) }
expect(logdev.string).to eq("45678")
end

context "long log lines" do
it "truncates log" do
logdev.write("Hello world, this is me!")
expect(logdev.string).to eq("Hello world, this...")
end

context "line with newline in the end" do
it "truncates log and adds newline" do
logdev.write("Hello world, this is me!\n")
expect(logdev.string).to eq("Hello world, thi...\n")
end
end
end
end
Loading