-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Periodically save the current buffers to a file, load on start
Persist the current circular buffers every `:persist_seconds`. When the backend starts, try to load the persisted logs and reinsert into the configured buffers.
- Loading branch information
Showing
7 changed files
with
254 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
defmodule RingLogger.Persistence do | ||
@moduledoc false | ||
|
||
@spec load(String.t()) :: [RingLogger.entry()] | {:error, atom()} | ||
def load(path) do | ||
path | ||
|> File.read!() | ||
|> :erlang.binary_to_term() | ||
rescue | ||
error in File.Error -> | ||
{:error, error.reason} | ||
|
||
ArgumentError -> | ||
{:error, :corrupted} | ||
end | ||
|
||
@spec save(String.t(), [RingLogger.entry()]) :: :ok | {:error, atom()} | ||
def save(path, logs) do | ||
File.write!(path, :erlang.term_to_binary(logs)) | ||
rescue | ||
error in File.Error -> | ||
{:error, error.reason} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
defmodule RingLogger.PersistenceTest do | ||
use ExUnit.Case, async: false | ||
|
||
alias RingLogger.Persistence | ||
|
||
test "saving logs" do | ||
File.rm("test/persistence.log") | ||
|
||
logs = [ | ||
%{ | ||
level: :debug, | ||
module: Logger, | ||
message: "Foo", | ||
timestamp: {{2023, 2, 8}, {13, 58, 31, 343}}, | ||
metadata: [] | ||
}, | ||
%{ | ||
level: :debug, | ||
module: Logger, | ||
message: "Bar", | ||
timestamp: {{2023, 2, 8}, {13, 58, 31, 343}}, | ||
metadata: [] | ||
} | ||
] | ||
|
||
:ok = Persistence.save("test/persistence.log", logs) | ||
|
||
assert File.exists?("test/persistence.log") | ||
|
||
File.rm("test/persistence.log") | ||
end | ||
|
||
test "loading logs" do | ||
File.rm("test/persistence.log") | ||
|
||
logs = [ | ||
%{ | ||
level: :debug, | ||
module: Logger, | ||
message: "Foo", | ||
timestamp: {{2023, 2, 8}, {13, 58, 31, 343}}, | ||
metadata: [] | ||
}, | ||
%{ | ||
level: :debug, | ||
module: Logger, | ||
message: "Bar", | ||
timestamp: {{2023, 2, 8}, {13, 58, 31, 343}}, | ||
metadata: [] | ||
} | ||
] | ||
|
||
:ok = Persistence.save("test/persistence.log", logs) | ||
|
||
loaded_logs = Persistence.load("test/persistence.log") | ||
|
||
assert logs == loaded_logs | ||
|
||
File.rm("test/persistence.log") | ||
end | ||
|
||
test "file was corrupted" do | ||
File.write!("test/persistence.log", "bad file") | ||
|
||
assert {:error, :corrupted} = Persistence.load("test/persistence.log") | ||
|
||
File.rm("test/persistence.log") | ||
end | ||
|
||
test "file doesn't exist" do | ||
assert {:error, :enoent} = Persistence.load("test/persistence.log") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters