Skip to content

Commit

Permalink
logging: Add possibility to log into file
Browse files Browse the repository at this point in the history
Add processing of environment variables LOGPATH and LOG_PATH
If either one exists - logger will log into the file on that
path instead of STDOUT.

refs: cnti-testcatalog#2000

Signed-off-by: Konstantin Yarovoy <[email protected]>
  • Loading branch information
Konstantin Yarovoy committed May 2, 2024
1 parent f812650 commit 622d7d7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
7 changes: 7 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ crystal src/cnf-testsuite.cr <testname>

---

### Logging Parameters

- **LOG_LEVEL** environment variable: sets minimal log level to display: error (default); info; debug.
- **LOG_PATH** environment variable: if set - all logs would be appended to the file defined by that variable.

---

### Common Example Commands

#### Building the executable
Expand Down
12 changes: 12 additions & 0 deletions spec/utils/utils_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,18 @@ describe "Utils" do
CNFManager.sample_cleanup(config_file: "sample-cnfs/sample-coredns-cnf", verbose: true)
end

it "'logger' should write logs to the file when LOG_PATH is set", tags: ["logger"] do
response_s = `LOG_PATH=spec-test-testsuite.log ./cnf-testsuite test`
$?.success?.should be_true
(/ERROR -- cnf-testsuite: error test/ =~ response_s).should be_nil
File.exists?("spec-test-testsuite.log").should be_true
(/ERROR -- cnf-testsuite: error test/ =~ File.read("spec-test-testsuite.log")).should_not be_nil
ensure
if File.exists?("spec-test-testsuite.log")
File.delete("spec-test-testsuite.log")
end
end

it "'#update_yml' should update the value for a key in a yml file", tags: ["logger"] do
begin
update_yml("spec/fixtures/cnf-testsuite.yml", "release_name", "coredns --set worker-node='kind-control-plane'")
Expand Down
19 changes: 17 additions & 2 deletions src/tasks/utils/utils.cr
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,24 @@ end

# this first line necessary to make sure our custom formatter
# is used in the default error log line also
Log.setup(Log::Severity::Error, Log::IOBackend.new(formatter: log_formatter))
Log.setup(loglevel, Log::IOBackend.new(formatter: log_formatter))
Log.setup(Log::Severity::Error, log_backend)
Log.setup(loglevel, log_backend)

def log_backend
if ENV.has_key?("LOGPATH") || ENV.has_key?("LOG_PATH")
log_file = ENV.has_key?("LOGPATH") ? ENV["LOGPATH"] : ENV["LOG_PATH"]
else
log_file = ""
end

if log_file.empty?
backend = Log::IOBackend.new(formatter: log_formatter)
else
log_io = File.open(log_file, "a")
backend = Log::IOBackend.new(io: log_io, formatter: log_formatter)
end
backend
end

def loglevel
levelstr = "" # default to unset
Expand Down

0 comments on commit 622d7d7

Please sign in to comment.