logsafe 0.1.0 → 0.3.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: 54eebb94b02703a3b0374a60e0ad7094a4e540dc89a1c33a3009a87e3b9bc9ff
4
+ data.tar.gz: b5a4874d9c759f06c735ba2c850e7f19a4aa9ee474c341592786eb1a11b9b547
5
5
  SHA512:
6
- metadata.gz: 214f4fda72b76e2a2ea3ea4f8e2d35083640b17c4834aedfdfb9cd07f77e2e526002d4fb2d0017f71edd9081e2e0b3ef30cf443655f350b44c5f5904b23dd3f6
7
- data.tar.gz: 0f54a4914ebe5a008a26cfabd54924386ad6025fadf305de8133dd6bb58aa6159cb72e41a00c07ef14a23ad95805f080b14ba54855d61fd8fa02ec8a529ee5b4
6
+ metadata.gz: b4e077a0e491b3eee75fcba43279495eb0fea89767c44104a360aca7ed7a922946ec171752378491053138e7dd92c92258ade0fe5fc6a8f0df2118aa8c326f3a
7
+ data.tar.gz: 7570a6567a783e7462586a13dac0236a98cbaedcb79d1fa4d85152f7035e17f7eb50ee4b44a766e1112934326075a461e4e522e3163c6470bbac663eab2c7f39
data/CHANGELOG.md CHANGED
@@ -3,6 +3,25 @@
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.3.0] - 2026-11-25
7
+
8
+ - **In a bucket** the logs are now in the folder `logs/`: `logs/2026/11/<stream>.<date>.log.gz` and
9
+ `logs/parts/<stream>/<date>/`. Before, they were at the top of the bucket. `prefix:` in
10
+ `Logsafe.storage` names another folder, or none. A folder as storage, and one of your own, stay as
11
+ they were.
12
+
13
+ **If you already have logs in a bucket,** either keep them where they are, with `prefix: ""` and the
14
+ `key_prefix` you have in Vector, or move them: put `logs/` in front of `parts/` in Vector's
15
+ `key_prefix` and in the policy of its key, restart Vector, update the gem, and move `parts/` and the
16
+ year folders into `logs/`. What is not below `logs/` is neither merged nor shown.
17
+
18
+ ## [0.2.0] - 2026-11-25
19
+
20
+ - **In development** a Rails app that sets no `Logsafe.storage` now has its own log on the page, without
21
+ any setting. What the app logs is kept in `tmp/logsafe` as well as in `log/development.log`, for three
22
+ days. Set `Logsafe.storage` yourself and nothing of this happens. Before, the page said that there was
23
+ no storage, and everywhere else it still does.
24
+
6
25
  ## [0.1.0] - 2026-11-25
7
26
 
8
27
  The first version.
data/README.md CHANGED
@@ -11,10 +11,10 @@ do the rest:
11
11
  3. `Logsafe::MergeJob`, a background job in your Rails app, turns each finished day into a single file.
12
12
 
13
13
  ```
14
- my-logs/
15
- 2026/11/shop-prod.2026-11-23.log.gz one file per stream per day
16
- 2026/11/shop-prod.2026-11-24.log.gz
17
- parts/shop-prod/2026-11-25/… today, not merged yet
14
+ my-bucket/
15
+ logs/2026/11/shop-prod.2026-11-23.log.gz one file per stream per day
16
+ logs/2026/11/shop-prod.2026-11-24.log.gz
17
+ logs/parts/shop-prod/2026-11-25/… today, not merged yet
18
18
  ```
19
19
 
20
20
  A *stream* is a name you choose, such as `shop-prod` or `shop-prod-worker`. Containers with the same
@@ -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
 
@@ -80,21 +77,22 @@ Start the app and open `/logsafe`. For your own logs, go on with Setup.
80
77
  - Keep it private and turn **versioning** on, so a deleted or overwritten file can be brought back. Add
81
78
  a lifecycle rule that removes old versions after a week or two.
82
79
  - Use one bucket for production and another for staging. Let staging expire after a few weeks.
83
- - Make one access key per environment for the app servers, allowed only to write under `parts/`. On
80
+ - Make one access key per environment for the app servers, allowed only to write under `logs/parts/`. On
84
81
  AWS:
85
82
 
86
83
  ```json
87
84
  {
88
85
  "Version": "2012-10-17",
89
86
  "Statement": [
90
- { "Effect": "Allow", "Action": "s3:PutObject", "Resource": "arn:aws:s3:::my-logs/parts/shop-prod*" }
87
+ { "Effect": "Allow", "Action": "s3:PutObject", "Resource": "arn:aws:s3:::my-bucket/logs/parts/shop-prod*" }
91
88
  ]
92
89
  }
93
90
  ```
94
91
 
95
92
  A server that is broken into can then neither read nor delete logs. It can still add lines of its
96
93
  own, under any stream the key covers.
97
- - Make one more key for the merge job, allowed to list, read, write and delete in the bucket.
94
+ - Make one more key for the merge job, allowed to list the bucket, and to read, write and delete
95
+ under `logs/`.
98
96
 
99
97
  **A folder (a disk on your own server, a network share, or your own machine in development)**
100
98
 
@@ -213,14 +211,18 @@ gem "google-cloud-storage" # for Google Cloud Storage
213
211
 
214
212
  ```ruby
215
213
  # config/initializers/logsafe.rb, one of:
216
- Logsafe.storage = { s3: "my-logs", region: "eu-west-1" }
217
- Logsafe.storage = { s3: "my-logs", region: "ams3", endpoint: "https://ams3.digitaloceanspaces.com",
214
+ Logsafe.storage = { s3: "my-bucket", region: "eu-west-1" }
215
+ Logsafe.storage = { s3: "my-bucket", region: "ams3", endpoint: "https://ams3.digitaloceanspaces.com",
218
216
  access_key_id: "…", secret_access_key: "…" }
219
- Logsafe.storage = { gcs: "my-logs", project_id: "my-project", credentials: "/path/to/key.json" }
217
+ Logsafe.storage = { gcs: "my-bucket", project_id: "my-project", credentials: "/path/to/key.json" }
220
218
  Logsafe.storage = "/mnt/logs"
221
219
  ```
222
220
 
223
- What follows the bucket's name goes to `Aws::S3::Resource.new` or `Google::Cloud::Storage.new` as it
221
+ In a bucket the logs are in the folder `logs/`, beside whatever else it has. For another folder, add
222
+ `prefix: "shop/logs/"`, or `prefix: ""` for none, and put the same in front of `parts/` in Vector's
223
+ `key_prefix`: what Vector writes anywhere else is never found. A path is taken as it is.
224
+
225
+ What else follows the bucket's name goes to `Aws::S3::Resource.new` or `Google::Cloud::Storage.new` as it
224
226
  is. Without keys, each library looks where it usually does, such as `AWS_ACCESS_KEY_ID`. For storage
225
227
  of another kind, give `Logsafe.storage` an object of your own; the five methods it needs are listed in
226
228
  [lib/logsafe/storage.rb](lib/logsafe/storage.rb).
@@ -295,13 +297,13 @@ lists the bucket.
295
297
  ## Reading the logs
296
298
 
297
299
  ```sh
298
- aws s3 cp s3://my-logs/2026/11/shop-prod.2026-11-24.log.gz - | gzip -dc | grep "OrdersController"
300
+ aws s3 cp s3://my-bucket/logs/2026/11/shop-prod.2026-11-24.log.gz - | gzip -dc | grep "OrdersController"
299
301
  ```
300
302
 
301
303
  Today's lines are still in parts:
302
304
 
303
305
  ```sh
304
- aws s3 cp s3://my-logs/parts/shop-prod/$(date -u +%F)/ today --recursive
306
+ aws s3 cp s3://my-bucket/logs/parts/shop-prod/$(date -u +%F)/ today --recursive
305
307
  gzip -dc today/* | LC_ALL=C sort | less
306
308
  ```
307
309
 
@@ -429,7 +431,7 @@ To read a day, with the key in `LOGSAFE_KEY`. `logsafe` is the gem's command: `b
429
431
  in the app, or plain `logsafe` after `gem install logsafe` on your own machine.
430
432
 
431
433
  ```sh
432
- aws s3 cp s3://my-logs/2026/11/shop-prod.2026-11-24.log.gz.enc day.enc
434
+ aws s3 cp s3://my-bucket/logs/2026/11/shop-prod.2026-11-24.log.gz.enc day.enc
433
435
  logsafe decrypt day.enc | gzip -dc | grep "OrdersController"
434
436
  ```
435
437
 
@@ -437,7 +439,7 @@ Today's lines, from the parts, need the stream and the day as well. A part that
437
439
  named, and the others are printed.
438
440
 
439
441
  ```sh
440
- aws s3 cp s3://my-logs/parts/shop-prod/$(date -u +%F)/ today --recursive
442
+ aws s3 cp s3://my-bucket/logs/parts/shop-prod/$(date -u +%F)/ today --recursive
441
443
  logsafe decrypt shop-prod $(date -u +%F) today/* | LC_ALL=C sort | less
442
444
  ```
443
445
 
@@ -525,7 +527,7 @@ Set them in `config/initializers/logsafe.rb`. None is read from the environment
525
527
 
526
528
  | Setting | What it takes | Without it |
527
529
  | --- | --- | --- |
528
- | `Logsafe.storage` | a path, `{ folder: path }`, `{ s3: bucket, … }`, `{ gcs: bucket, … }` or [an object of your own](#storage-of-your-own) | nothing works |
530
+ | `Logsafe.storage` | a path, `{ folder: path }`, `{ s3: bucket, prefix: "logs/", … }`, `{ gcs: bucket, prefix: "logs/", … }` 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
531
  | `Logsafe.streams` | the names of the streams that are merged, shown and encrypted again | every stream |
530
532
  | `Logsafe.key` | a key from `logsafe key` | nothing is encrypted, and what is cannot be read |
531
533
  | `Logsafe.old_keys` | keys that were in use before: a list, or one text with commas between them | only `Logsafe.key` reads |
@@ -567,26 +569,6 @@ Any object with these five methods can be `Logsafe.storage`. What each has to pr
567
569
 
568
570
  Give it a `to_s` that says which storage it is, so that the page keeps two of them apart.
569
571
 
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
572
  ## Good to know
591
573
 
592
574
  - **One file per day** holds once the day is over and two merges have run. During the day there are
@@ -628,7 +610,7 @@ time, with each kind of storage.
628
610
 
629
611
  By hand, with the built gem installed: the page mounted in a Rails app, in a namespace and behind the
630
612
  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.
613
+ with Ruby 4.0. What the page finds of lograge's lines, on Rails 8.1. The default in development, on both.
632
614
 
633
615
  Not run: the Devise example; real AWS, Google or DigitalOcean storage; Vector sending to Google Cloud Storage (the stand-in
634
616
  refuses the kind of upload Vector makes); Kamal; the scheduling examples above, which follow each
@@ -649,6 +631,22 @@ To publish a version: set it in `lib/logsafe/version.rb`, run `bundle install`,
649
631
  [CHANGELOG.md](CHANGELOG.md), merge to `master`, wait for CI, and run `bundle exec rake release` there.
650
632
  That builds the gem, pushes the tag, and pushes the gem to RubyGems, which asks for a second factor.
651
633
 
634
+ ## Disclaimer
635
+
636
+ Logsafe comes as is, without warranty of any kind, and its authors are not liable for any loss or damage
637
+ from using it. The [license](LICENSE.txt) has the exact terms.
638
+
639
+ Log lines can be lost, for instance when a server's disk dies before Vector has sent them, or when a
640
+ container is removed while Vector is down. Don't make Logsafe the only place for anything you have to
641
+ keep, such as an audit trail or records the law requires. Save those in your database.
642
+
643
+ Try it on your own setup before you depend on it: [What has been run](#what-has-been-run) says what has
644
+ been tested and what has not. Your keys, who can reach the page and the storage, the personal data in
645
+ your logs and the storage bill are your responsibility. If you lose the encryption key, nobody can
646
+ recover the logs.
647
+
648
+ Logsafe is not affiliated with or endorsed by the makers of the products named in this README.
649
+
652
650
  ## License
653
651
 
654
652
  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
@@ -58,12 +58,17 @@ module Logsafe
58
58
  case setting
59
59
  in String | Pathname then Folder.new(setting.to_s)
60
60
  in { folder: } then Folder.new(folder.to_s)
61
- in { s3: String => bucket, **options } then S3.new(S3.bucket(bucket, options))
62
- in { gcs: String => bucket, **options } then GCS.new(GCS.bucket(bucket, options))
61
+ in { s3: String => bucket, **options } then in_bucket(S3, bucket, **options)
62
+ in { gcs: String => bucket, **options } then in_bucket(GCS, bucket, **options)
63
63
  else setting
64
64
  end
65
65
  end
66
66
 
67
+ def self.in_bucket(type, name, prefix: Bucket::PREFIX, **options)
68
+ type.new(type.bucket(name, options), prefix:)
69
+ end
70
+ private_class_method :in_bucket
71
+
67
72
  # A folder: a local disk, a mounted volume or a network share.
68
73
  # What is below the root is opened one folder at a time, each from the handle of the one above, and
69
74
  # never through a symbolic link. Whoever may write to the folder, such as a server that was broken
@@ -219,33 +224,46 @@ module Logsafe
219
224
  end
220
225
  end
221
226
 
227
+ # What buckets have in common: the logs are in a folder there, beside whatever else the bucket has.
228
+ # The keys given and returned are those below it. Vector has to write to the same one: shipper/to-*.yaml.
229
+ class Bucket
230
+ PREFIX = "logs/"
231
+
232
+ def initialize(bucket, prefix: PREFIX)
233
+ @bucket = bucket
234
+ @prefix = prefix.to_s.split("/").reject(&:empty?).map { |step| "#{step}/" }.join
235
+ end
236
+
237
+ private
238
+
239
+ def full(key)
240
+ @prefix + key
241
+ end
242
+ end
243
+
222
244
  # Amazon S3 and anything that speaks its protocol: DigitalOcean Spaces, MinIO, Garage, ...
223
- class S3
245
+ class S3 < Bucket
224
246
  def self.bucket(name, options)
225
247
  require "aws-sdk-s3"
226
248
  Aws::S3::Resource.new(**options).bucket(name)
227
249
  end
228
250
 
229
- def initialize(bucket)
230
- @bucket = bucket
231
- end
232
-
233
251
  def keys(prefix)
234
- @bucket.objects(prefix:).map(&:key)
252
+ @bucket.objects(prefix: full(prefix)).map { |object| object.key.delete_prefix(@prefix) }
235
253
  end
236
254
 
237
255
  def to_s
238
- "s3:#{@bucket.client.config.endpoint}/#{@bucket.name}"
256
+ "s3:#{@bucket.client.config.endpoint}/#{@bucket.name}/#{@prefix}"
239
257
  end
240
258
 
241
259
  # In one request: left to itself the library asks for the size first, twice the requests for a
242
260
  # day's thousand parts. A file of several GB comes in one piece too, which is slower but works.
243
261
  def fetch(key, path)
244
- transfer.download_file(path, bucket: @bucket.name, key:, mode: "single_request")
262
+ transfer.download_file(path, bucket: @bucket.name, key: full(key), mode: "single_request")
245
263
  end
246
264
 
247
265
  def version(key)
248
- @bucket.object(key).etag
266
+ @bucket.object(full(key)).etag
249
267
  rescue Aws::S3::Errors::NotFound
250
268
  nil
251
269
  end
@@ -265,7 +283,8 @@ module Logsafe
265
283
 
266
284
  def delete(keys)
267
285
  keys.each_slice(1000) do |slice|
268
- failed = @bucket.delete_objects(delete: { objects: slice.map { |key| { key: } }, quiet: true }).errors
286
+ objects = slice.map { |key| { key: full(key) } }
287
+ failed = @bucket.delete_objects(delete: { objects:, quiet: true }).errors
269
288
  raise Error, "could not delete #{failed.first.key}: #{failed.first.message}" if failed.any?
270
289
  end
271
290
  end
@@ -273,7 +292,7 @@ module Logsafe
273
292
  private
274
293
 
275
294
  def upload(path, key, only_if)
276
- transfer.upload_file(path, bucket: @bucket.name, key:, **only_if)
295
+ transfer.upload_file(path, bucket: @bucket.name, key: full(key), **only_if)
277
296
  end
278
297
 
279
298
  def codes(error)
@@ -288,36 +307,32 @@ module Logsafe
288
307
  end
289
308
 
290
309
  # Google Cloud Storage.
291
- class GCS
310
+ class GCS < Bucket
292
311
  def self.bucket(name, options)
293
312
  require "google/cloud/storage"
294
313
  Google::Cloud::Storage.new(**options).bucket(name, skip_lookup: true)
295
314
  end
296
315
 
297
- def initialize(bucket)
298
- @bucket = bucket
299
- end
300
-
301
316
  def keys(prefix)
302
- @bucket.files(prefix:).all.map(&:name)
317
+ @bucket.files(prefix: full(prefix)).all.map { |file| file.name.delete_prefix(@prefix) }
303
318
  end
304
319
 
305
320
  def to_s
306
- "gcs:#{@bucket.name}"
321
+ "gcs:#{@bucket.name}/#{@prefix}"
307
322
  end
308
323
 
309
324
  # As it is stored: left to itself the library unpacks gzip in memory, however much that comes to.
310
325
  def fetch(key, path)
311
- @bucket.file(key, skip_lookup: true).download(path, skip_decompress: true)
326
+ @bucket.file(full(key), skip_lookup: true).download(path, skip_decompress: true)
312
327
  end
313
328
 
314
329
  def version(key)
315
- @bucket.file(key)&.generation
330
+ @bucket.file(full(key))&.generation
316
331
  end
317
332
 
318
333
  # The storage itself refuses to replace another version; 0 is one that is not there.
319
334
  def store(path, key, was)
320
- @bucket.create_file(path, key, if_generation_match: was || 0)
335
+ @bucket.create_file(path, full(key), if_generation_match: was || 0)
321
336
  rescue Google::Cloud::FailedPreconditionError
322
337
  raise Changed, key
323
338
  end
@@ -325,7 +340,7 @@ module Logsafe
325
340
  # One request per file, so several at a time. A file that is gone already, another run removed.
326
341
  def delete(keys)
327
342
  Storage.side_by_side(keys) do |key|
328
- @bucket.file(key, skip_lookup: true).delete
343
+ @bucket.file(full(key), skip_lookup: true).delete
329
344
  rescue Google::Cloud::NotFoundError
330
345
  next
331
346
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Logsafe
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/logsafe.rb CHANGED
@@ -8,7 +8,7 @@ require_relative "logsafe/version"
8
8
 
9
9
  # Turns the parts a log shipper left in storage into one sorted file per stream per day:
10
10
  # parts/<stream>/<date>/* -> <year>/<month>/<stream>.<date>.log.gz
11
- # With a key, encrypted: see Logsafe::Cipher.
11
+ # In a bucket both are below logs/. With a key, encrypted: see Logsafe::Cipher.
12
12
  module Logsafe
13
13
  class Error < StandardError; end
14
14
 
@@ -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)
data/shipper/to-gcs.yaml CHANGED
@@ -4,8 +4,8 @@ sinks:
4
4
  storage:
5
5
  type: gcp_cloud_storage
6
6
  inputs: [lines]
7
- bucket: my-logs
8
- key_prefix: "parts/{{ stream }}/{{ day }}/"
7
+ bucket: my-bucket
8
+ key_prefix: "logs/parts/{{ stream }}/{{ day }}/"
9
9
  encoding:
10
10
  codec: text
11
11
  framing:
data/shipper/to-s3.yaml CHANGED
@@ -4,10 +4,10 @@ sinks:
4
4
  storage:
5
5
  type: aws_s3
6
6
  inputs: [lines]
7
- bucket: my-logs
7
+ bucket: my-bucket
8
8
  region: us-east-1
9
9
  # endpoint: https://ams3.digitaloceanspaces.com
10
- key_prefix: "parts/{{ stream }}/{{ day }}/"
10
+ key_prefix: "logs/parts/{{ stream }}/{{ day }}/"
11
11
  encoding:
12
12
  codec: text
13
13
  framing:
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.3.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.3.0/README.md
72
+ changelog_uri: https://github.com/TTD93/LogSafe/blob/v0.3.0/CHANGELOG.md
71
73
  rubygems_mfa_required: 'true'
72
74
  rdoc_options: []
73
75
  require_paths: