logsafe 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b7d59f1ca7d708a4ea6923ed8726f78cf3bf7d5e3a2ecf5c96a642675af338a3
4
- data.tar.gz: 0b6595fa191856ae444777ac457cd865d51729c417a8180ce78fe7392d02972e
3
+ metadata.gz: 4c7eefda5cdcdc7c53ea49300c2d75fc427480c2a358577c1d1d6ef808c843d7
4
+ data.tar.gz: 2b24eb1682e7bf914c78078dd43016160665c218d3d80d4fa37c3d2eb2a700b6
5
5
  SHA512:
6
- metadata.gz: 214f4fda72b76e2a2ea3ea4f8e2d35083640b17c4834aedfdfb9cd07f77e2e526002d4fb2d0017f71edd9081e2e0b3ef30cf443655f350b44c5f5904b23dd3f6
7
- data.tar.gz: 0f54a4914ebe5a008a26cfabd54924386ad6025fadf305de8133dd6bb58aa6159cb72e41a00c07ef14a23ad95805f080b14ba54855d61fd8fa02ec8a529ee5b4
6
+ metadata.gz: 5af5cdf51480703d153c7185ee99bf961e77049a4f48c3bed648b6b6d425ddff503a8b678b6f0200dcacd9202c55b90826743b0b4e76066514f3ac42d403921d
7
+ data.tar.gz: a1479ebb1a1b3643ce0ab88c2ba343339be9c4bbc7535a91751c5b60aba74c9ab2808f86e52d93685267579225b5435adce2582f11c21195bd6661af86382b02
data/CHANGELOG.md CHANGED
@@ -3,6 +3,13 @@
3
3
  The versions follow [Semantic Versioning](https://semver.org). Until 1.0, a minor version can change
4
4
  what is in the README's Reference.
5
5
 
6
+ ## [0.2.0] - 2026-11-25
7
+
8
+ - **In development** a Rails app that sets no `Logsafe.storage` now has its own log on the page, without
9
+ any setting. What the app logs is kept in `tmp/logsafe` as well as in `log/development.log`, for three
10
+ days. Set `Logsafe.storage` yourself and nothing of this happens. Before, the page said that there was
11
+ no storage, and everywhere else it still does.
12
+
6
13
  ## [0.1.0] - 2026-11-25
7
14
 
8
15
  The first version.
data/README.md CHANGED
@@ -36,8 +36,8 @@ You can also [search the logs in the browser](#in-the-browser), on a page you mo
36
36
 
37
37
  **Contents:** [Requirements](#requirements) · [Try it](#try-it) · [Setup](#setup) ·
38
38
  [Reading the logs](#reading-the-logs) · [Lograge and other loggers](#lograge-and-other-loggers) ·
39
- [Encryption](#encryption) · [Reference](#reference) · [What can still be lost](#what-can-still-be-lost) ·
40
- [Good to know](#good-to-know) · [What has been run](#what-has-been-run) · [Development](#development)
39
+ [Encryption](#encryption) · [Reference](#reference) · [Good to know](#good-to-know) ·
40
+ [What has been run](#what-has-been-run) · [Development](#development) · [Disclaimer](#disclaimer)
41
41
 
42
42
  ## Requirements
43
43
 
@@ -50,26 +50,23 @@ You can also [search the logs in the browser](#in-the-browser), on a page you mo
50
50
 
51
51
  ## Try it
52
52
 
53
- A look at the page on your own machine, with a folder for storage and no Vector:
53
+ A look at the page on your own machine, without Vector or a bucket:
54
54
 
55
55
  ```sh
56
56
  bundle add logsafe
57
-
58
- day=$(date -u +%F)
59
- mkdir -p tmp/logs/parts/demo/$day
60
- echo "${day}T08:00:00.000000001Z ERROR hello" > tmp/logs/parts/demo/$day/a.log
61
- touch -t 202601010000 tmp/logs/parts/demo/$day/a.log # a part that was just written is left alone
62
57
  ```
63
58
 
64
59
  ```ruby
65
- # config/initializers/logsafe.rb
66
- Logsafe.storage = Rails.root.join("tmp/logs")
67
-
68
60
  # config/routes.rb
69
61
  mount Logsafe::Web, at: "logsafe" if Rails.env.development?
70
62
  ```
71
63
 
72
- Start the app and open `/logsafe`. For your own logs, go on with Setup.
64
+ Start the app and open `/logsafe`: it shows what your app logs, as soon as it is logged. For the logs of
65
+ your servers, go on with Setup.
66
+
67
+ In development, a Rails app that sets no `Logsafe.storage` gets this by itself. Its log still goes where
68
+ it went before, and also to `tmp/logsafe`, where three days are kept. Each program, such as `rails` or
69
+ `sidekiq`, is a stream. Set `Logsafe.storage` and none of this happens.
73
70
 
74
71
  ## Setup
75
72
 
@@ -525,7 +522,7 @@ Set them in `config/initializers/logsafe.rb`. None is read from the environment
525
522
 
526
523
  | Setting | What it takes | Without it |
527
524
  | --- | --- | --- |
528
- | `Logsafe.storage` | a path, `{ folder: path }`, `{ s3: bucket, … }`, `{ gcs: bucket, … }` or [an object of your own](#storage-of-your-own) | nothing works |
525
+ | `Logsafe.storage` | a path, `{ folder: path }`, `{ s3: bucket, … }`, `{ gcs: bucket, … }` or [an object of your own](#storage-of-your-own) | in development, a Rails app has [its own log](#try-it); elsewhere nothing works |
529
526
  | `Logsafe.streams` | the names of the streams that are merged, shown and encrypted again | every stream |
530
527
  | `Logsafe.key` | a key from `logsafe key` | nothing is encrypted, and what is cannot be read |
531
528
  | `Logsafe.old_keys` | keys that were in use before: a list, or one text with commas between them | only `Logsafe.key` reads |
@@ -567,26 +564,6 @@ Any object with these five methods can be `Logsafe.storage`. What each has to pr
567
564
 
568
565
  Give it a `to_s` that says which storage it is, so that the page keeps two of them apart.
569
566
 
570
- ## What can still be lost
571
-
572
- | What happens | What is lost |
573
- | --- | --- |
574
- | The app crashes or is killed | Nothing. A line written to STDOUT has already left the process. |
575
- | The network or the storage is down | Nothing, for as long as it fits on the server: 1 GB in Vector's buffer, then Docker's own log files up to their limit. |
576
- | The merge job or the job backend is down | Nothing. The parts wait. |
577
- | The server loses power | The last few seconds, which the system had not yet written to disk. |
578
- | The server's disk is destroyed | What was not sent yet: up to a minute. |
579
- | A container is removed (`docker rm`, `--rm`, a deploy tidying up) | Its log file goes with it. Normally Vector has read all but the last second or less. If Vector was down, everything it had not read from that container. |
580
- | Vector is down for a long time | Lines that Docker's rotation (`max-size` × `max-file`) removed meanwhile. |
581
- | The bucket's name or the key is wrong | What Vector tried to send meanwhile. While the lines are still in Docker's log files you can get them back: correct the setting, remove the `vector-data` volume and start Vector again. It ships everything again, and the merge drops what it already has. |
582
- | A file in the bucket is deleted or overwritten | Nothing, with versioning on. |
583
-
584
- So watch that Vector is running and not logging errors, not only that the merge succeeds.
585
-
586
- Nothing can promise that no line is ever lost without making the app wait for each line to be stored.
587
- What must never be lost, such as an audit trail, belongs in your database, saved together with the
588
- change it describes.
589
-
590
567
  ## Good to know
591
568
 
592
569
  - **One file per day** holds once the day is over and two merges have run. During the day there are
@@ -628,7 +605,7 @@ time, with each kind of storage.
628
605
 
629
606
  By hand, with the built gem installed: the page mounted in a Rails app, in a namespace and behind the
630
607
  session constraint above, and the merge through Active Job, on Rails 7.1 with Ruby 3.3 and on Rails 8.1
631
- with Ruby 4.0. What the page finds of lograge's lines, on Rails 8.1.
608
+ with Ruby 4.0. What the page finds of lograge's lines, on Rails 8.1. The default in development, on both.
632
609
 
633
610
  Not run: the Devise example; real AWS, Google or DigitalOcean storage; Vector sending to Google Cloud Storage (the stand-in
634
611
  refuses the kind of upload Vector makes); Kamal; the scheduling examples above, which follow each
@@ -649,6 +626,22 @@ To publish a version: set it in `lib/logsafe/version.rb`, run `bundle install`,
649
626
  [CHANGELOG.md](CHANGELOG.md), merge to `master`, wait for CI, and run `bundle exec rake release` there.
650
627
  That builds the gem, pushes the tag, and pushes the gem to RubyGems, which asks for a second factor.
651
628
 
629
+ ## Disclaimer
630
+
631
+ Logsafe comes as is, without warranty of any kind, and its authors are not liable for any loss or damage
632
+ from using it. The [license](LICENSE.txt) has the exact terms.
633
+
634
+ Log lines can be lost, for instance when a server's disk dies before Vector has sent them, or when a
635
+ container is removed while Vector is down. Don't make Logsafe the only place for anything you have to
636
+ keep, such as an audit trail or records the law requires. Save those in your database.
637
+
638
+ Try it on your own setup before you depend on it: [What has been run](#what-has-been-run) says what has
639
+ been tested and what has not. Your keys, who can reach the page and the storage, the personal data in
640
+ your logs and the storage bill are your responsibility. If you lose the encryption key, nobody can
641
+ recover the logs.
642
+
643
+ Logsafe is not affiliated with or endorsed by the makers of the products named in this README.
644
+
652
645
  ## License
653
646
 
654
647
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ module Logsafe
6
+ # The storage of a Rails app in development that sets none. No Vector runs on a developer's machine, so
7
+ # the app's log comes here too, beside where it goes otherwise, and the page has something to show.
8
+ # It is what a logger writes to, and the folder the page reads. Lines first go to an open file, which
9
+ # becomes a part when the parts are listed: the page reads each part once, so it has to be finished.
10
+ # ponytail: a line another process is writing at that very moment can miss the page until its copy is
11
+ # gone. A lock around both, if that is ever felt.
12
+ class Development < Storage::Folder
13
+ KEEP = 3 # days, with today
14
+ OPEN_FILES = "open"
15
+ COLOURS = /\e\[[\d;]*m/
16
+
17
+ # Streams are called after the program that runs, such as rails or sidekiq. The names an app gives
18
+ # its streams are those of its containers, so here they would hide everything.
19
+ def self.start(root, logger, program: $PROGRAM_NAME)
20
+ logs = new(root, File.basename(program)[/\A#{STREAM}/] || "app")
21
+ logs.prune
22
+ Logsafe.streams = nil
23
+ Logsafe.storage = logs
24
+ logger.broadcast_to(ActiveSupport::Logger.new(logs, level: logger.level, formatter: ::Logger::Formatter.new))
25
+ end
26
+
27
+ def initialize(root, stream)
28
+ super(root.to_s)
29
+ @stream = stream
30
+ @last = 0
31
+ end
32
+
33
+ # A message, from a logger, which lets one thread in at a time. Each of its lines gets a time of its
34
+ # own, so that they stay in order: what is sorted later is the text.
35
+ def write(message)
36
+ text = message.dup.force_encoding(Encoding::UTF_8).scrub.gsub(COLOURS, "")
37
+ text.lines(chomp: true).reject { |line| line.strip.empty? }.each { |line| add(line) }
38
+ end
39
+
40
+ # A logger asks that of what it writes to. Nothing is kept open here.
41
+ def close; end
42
+
43
+ def keys(prefix)
44
+ Dir.glob(File.join(@root, OPEN_FILES, "*", "*.log")).each { |file| finish(file) } if prefix == PARTS
45
+ super
46
+ end
47
+
48
+ # Deletes what is older than KEEP days.
49
+ def prune
50
+ oldest = (Time.now.utc.to_date - (KEEP - 1)).to_s
51
+ Dir.glob(File.join(@root, "{#{PARTS}*,#{OPEN_FILES}}", "*")).each do |dir|
52
+ day = File.basename(dir)
53
+ FileUtils.rm_rf(dir) if day.match?(DATE) && day < oldest
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def add(line)
60
+ @last = [now, @last + 1].max
61
+ time = Time.at(0, @last, :nsec).utc
62
+ dir = File.join(@root, OPEN_FILES, time.strftime("%F"))
63
+ FileUtils.mkdir_p(dir, mode: 0o700)
64
+ line = "#{time.strftime("%FT%T.%9NZ")} #{line}\n"
65
+ File.open(File.join(dir, "#{@stream}.log"), "a", 0o600) { |file| file.write(line) }
66
+ end
67
+
68
+ def finish(file)
69
+ dir = File.join(@root, PARTS, File.basename(file, ".log"), File.basename(File.dirname(file)))
70
+ FileUtils.mkdir_p(dir, mode: 0o700)
71
+ File.rename(file, File.join(dir, "#{now}-#{Process.pid}.log"))
72
+ rescue Errno::ENOENT
73
+ nil # another process was first
74
+ end
75
+
76
+ def now
77
+ Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
78
+ end
79
+
80
+ # A part here is finished, however fresh it is.
81
+ def settled?(_key, stat)
82
+ stat.file?
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Logsafe
4
+ # In development an app that sets no storage gets one, after its own initializers had their say.
5
+ class Railtie < Rails::Railtie
6
+ config.after_initialize do
7
+ if Rails.env.development? && !Logsafe.storage? && Rails.logger.respond_to?(:broadcast_to)
8
+ Development.start(Rails.root.join("tmp/logsafe"), Rails.logger)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -77,10 +77,11 @@ module Logsafe
77
77
  # for a while. A key that is gone by the time it is fetched is noticed there.
78
78
  # One thread at a time: a server's threads share the listings, and a second search that comes
79
79
  # while the storage is being listed waits for that listing and does not ask for its own.
80
+ # In development every search lists, so that a line is there as soon as it is logged.
80
81
  def listed(prefix)
81
82
  LISTING.synchronize do
82
83
  at, keys = (@listed ||= {})[[Logsafe.storage, prefix]]
83
- next keys if at && Time.now - at < LIST_FOR
84
+ next keys if at && Time.now - at < LIST_FOR && !Logsafe.storage.is_a?(Development)
84
85
 
85
86
  @listed.delete_if { |_, (time, _)| Time.now - time >= LIST_FOR }
86
87
  (@listed[[Logsafe.storage, prefix]] = [Time.now, Logsafe.storage.keys(prefix)]).last
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Logsafe
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/logsafe.rb CHANGED
@@ -16,6 +16,7 @@ module Logsafe
16
16
  autoload :Reencrypt, "logsafe/reencrypt"
17
17
  autoload :ReencryptJob, "logsafe/reencrypt_job"
18
18
  autoload :Copies, "logsafe/copies"
19
+ autoload :Development, "logsafe/development"
19
20
  autoload :Search, "logsafe/search"
20
21
  autoload :Web, "logsafe/web"
21
22
 
@@ -39,6 +40,10 @@ module Logsafe
39
40
  @storage || raise(Error, "Logsafe.storage is not set, see the README")
40
41
  end
41
42
 
43
+ def storage?
44
+ !@storage.nil?
45
+ end
46
+
42
47
  # The names of the streams there are, if nothing else is to be merged, shown or encrypted again.
43
48
  # Whoever may write parts can make up any number of other names.
44
49
  def streams=(names)
@@ -157,3 +162,4 @@ require_relative "logsafe/storage"
157
162
  require_relative "logsafe/cipher"
158
163
  require_relative "logsafe/source"
159
164
  require_relative "logsafe/day"
165
+ require_relative "logsafe/railtie" if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logsafe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tin.trung.duong
@@ -44,8 +44,10 @@ files:
44
44
  - lib/logsafe/cipher.rb
45
45
  - lib/logsafe/copies.rb
46
46
  - lib/logsafe/day.rb
47
+ - lib/logsafe/development.rb
47
48
  - lib/logsafe/merge_job.rb
48
49
  - lib/logsafe/private_folder.rb
50
+ - lib/logsafe/railtie.rb
49
51
  - lib/logsafe/reencrypt.rb
50
52
  - lib/logsafe/reencrypt_job.rb
51
53
  - lib/logsafe/room.rb
@@ -66,8 +68,8 @@ licenses:
66
68
  metadata:
67
69
  source_code_uri: https://github.com/TTD93/LogSafe
68
70
  bug_tracker_uri: https://github.com/TTD93/LogSafe/issues
69
- documentation_uri: https://github.com/TTD93/LogSafe/blob/v0.1.0/README.md
70
- changelog_uri: https://github.com/TTD93/LogSafe/blob/v0.1.0/CHANGELOG.md
71
+ documentation_uri: https://github.com/TTD93/LogSafe/blob/v0.2.0/README.md
72
+ changelog_uri: https://github.com/TTD93/LogSafe/blob/v0.2.0/CHANGELOG.md
71
73
  rubygems_mfa_required: 'true'
72
74
  rdoc_options: []
73
75
  require_paths: