logsafe 0.2.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 +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +21 -16
- data/lib/logsafe/storage.rb +39 -24
- data/lib/logsafe/version.rb +1 -1
- data/lib/logsafe.rb +1 -1
- data/shipper/to-gcs.yaml +2 -2
- data/shipper/to-s3.yaml +2 -2
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 54eebb94b02703a3b0374a60e0ad7094a4e540dc89a1c33a3009a87e3b9bc9ff
|
|
4
|
+
data.tar.gz: b5a4874d9c759f06c735ba2c850e7f19a4aa9ee474c341592786eb1a11b9b547
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b4e077a0e491b3eee75fcba43279495eb0fea89767c44104a360aca7ed7a922946ec171752378491053138e7dd92c92258ade0fe5fc6a8f0df2118aa8c326f3a
|
|
7
|
+
data.tar.gz: 7570a6567a783e7462586a13dac0236a98cbaedcb79d1fa4d85152f7035e17f7eb50ee4b44a766e1112934326075a461e4e522e3163c6470bbac663eab2c7f39
|
data/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,18 @@
|
|
|
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
|
+
|
|
6
18
|
## [0.2.0] - 2026-11-25
|
|
7
19
|
|
|
8
20
|
- **In development** a Rails app that sets no `Logsafe.storage` now has its own log on the page, without
|
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-
|
|
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
|
|
@@ -77,21 +77,22 @@ it went before, and also to `tmp/logsafe`, where three days are kept. Each progr
|
|
|
77
77
|
- Keep it private and turn **versioning** on, so a deleted or overwritten file can be brought back. Add
|
|
78
78
|
a lifecycle rule that removes old versions after a week or two.
|
|
79
79
|
- Use one bucket for production and another for staging. Let staging expire after a few weeks.
|
|
80
|
-
- 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
|
|
81
81
|
AWS:
|
|
82
82
|
|
|
83
83
|
```json
|
|
84
84
|
{
|
|
85
85
|
"Version": "2012-10-17",
|
|
86
86
|
"Statement": [
|
|
87
|
-
{ "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*" }
|
|
88
88
|
]
|
|
89
89
|
}
|
|
90
90
|
```
|
|
91
91
|
|
|
92
92
|
A server that is broken into can then neither read nor delete logs. It can still add lines of its
|
|
93
93
|
own, under any stream the key covers.
|
|
94
|
-
- Make one more key for the merge job, allowed to list, read, write and delete
|
|
94
|
+
- Make one more key for the merge job, allowed to list the bucket, and to read, write and delete
|
|
95
|
+
under `logs/`.
|
|
95
96
|
|
|
96
97
|
**A folder (a disk on your own server, a network share, or your own machine in development)**
|
|
97
98
|
|
|
@@ -210,14 +211,18 @@ gem "google-cloud-storage" # for Google Cloud Storage
|
|
|
210
211
|
|
|
211
212
|
```ruby
|
|
212
213
|
# config/initializers/logsafe.rb, one of:
|
|
213
|
-
Logsafe.storage = { s3: "my-
|
|
214
|
-
Logsafe.storage = { s3: "my-
|
|
214
|
+
Logsafe.storage = { s3: "my-bucket", region: "eu-west-1" }
|
|
215
|
+
Logsafe.storage = { s3: "my-bucket", region: "ams3", endpoint: "https://ams3.digitaloceanspaces.com",
|
|
215
216
|
access_key_id: "…", secret_access_key: "…" }
|
|
216
|
-
Logsafe.storage = { gcs: "my-
|
|
217
|
+
Logsafe.storage = { gcs: "my-bucket", project_id: "my-project", credentials: "/path/to/key.json" }
|
|
217
218
|
Logsafe.storage = "/mnt/logs"
|
|
218
219
|
```
|
|
219
220
|
|
|
220
|
-
|
|
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
|
|
221
226
|
is. Without keys, each library looks where it usually does, such as `AWS_ACCESS_KEY_ID`. For storage
|
|
222
227
|
of another kind, give `Logsafe.storage` an object of your own; the five methods it needs are listed in
|
|
223
228
|
[lib/logsafe/storage.rb](lib/logsafe/storage.rb).
|
|
@@ -292,13 +297,13 @@ lists the bucket.
|
|
|
292
297
|
## Reading the logs
|
|
293
298
|
|
|
294
299
|
```sh
|
|
295
|
-
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"
|
|
296
301
|
```
|
|
297
302
|
|
|
298
303
|
Today's lines are still in parts:
|
|
299
304
|
|
|
300
305
|
```sh
|
|
301
|
-
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
|
|
302
307
|
gzip -dc today/* | LC_ALL=C sort | less
|
|
303
308
|
```
|
|
304
309
|
|
|
@@ -426,7 +431,7 @@ To read a day, with the key in `LOGSAFE_KEY`. `logsafe` is the gem's command: `b
|
|
|
426
431
|
in the app, or plain `logsafe` after `gem install logsafe` on your own machine.
|
|
427
432
|
|
|
428
433
|
```sh
|
|
429
|
-
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
|
|
430
435
|
logsafe decrypt day.enc | gzip -dc | grep "OrdersController"
|
|
431
436
|
```
|
|
432
437
|
|
|
@@ -434,7 +439,7 @@ Today's lines, from the parts, need the stream and the day as well. A part that
|
|
|
434
439
|
named, and the others are printed.
|
|
435
440
|
|
|
436
441
|
```sh
|
|
437
|
-
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
|
|
438
443
|
logsafe decrypt shop-prod $(date -u +%F) today/* | LC_ALL=C sort | less
|
|
439
444
|
```
|
|
440
445
|
|
|
@@ -522,7 +527,7 @@ Set them in `config/initializers/logsafe.rb`. None is read from the environment
|
|
|
522
527
|
|
|
523
528
|
| Setting | What it takes | Without it |
|
|
524
529
|
| --- | --- | --- |
|
|
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 |
|
|
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 |
|
|
526
531
|
| `Logsafe.streams` | the names of the streams that are merged, shown and encrypted again | every stream |
|
|
527
532
|
| `Logsafe.key` | a key from `logsafe key` | nothing is encrypted, and what is cannot be read |
|
|
528
533
|
| `Logsafe.old_keys` | keys that were in use before: a list, or one text with commas between them | only `Logsafe.key` reads |
|
data/lib/logsafe/storage.rb
CHANGED
|
@@ -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
|
|
62
|
-
in { gcs: String => bucket, **options } then
|
|
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(
|
|
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
|
|
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
|
-
|
|
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
|
|
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(
|
|
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
|
data/lib/logsafe/version.rb
CHANGED
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
|
|
data/shipper/to-gcs.yaml
CHANGED
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-
|
|
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.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- tin.trung.duong
|
|
@@ -68,8 +68,8 @@ licenses:
|
|
|
68
68
|
metadata:
|
|
69
69
|
source_code_uri: https://github.com/TTD93/LogSafe
|
|
70
70
|
bug_tracker_uri: https://github.com/TTD93/LogSafe/issues
|
|
71
|
-
documentation_uri: https://github.com/TTD93/LogSafe/blob/v0.
|
|
72
|
-
changelog_uri: https://github.com/TTD93/LogSafe/blob/v0.
|
|
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
|
|
73
73
|
rubygems_mfa_required: 'true'
|
|
74
74
|
rdoc_options: []
|
|
75
75
|
require_paths:
|