Skip to content

Commit

Permalink
add max_log_length
Browse files Browse the repository at this point in the history
  • Loading branch information
tycooon committed Dec 12, 2024
1 parent 5f43f88 commit 4b6172b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
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
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
23 changes: 17 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,15 @@ 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 = "..."
msg = msg[0, max_log_length - suffix.size]

"#{msg}#{suffix}"
end
end
end
7 changes: 6 additions & 1 deletion spec/lamian/log_device_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# 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 ")
Expand All @@ -13,4 +13,9 @@
8.times { |x| logdev.write(x + 1) }
expect(logdev.string).to eq("45678")
end

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

0 comments on commit 4b6172b

Please sign in to comment.