Skip to content

Commit ec87f0f

Browse files
author
dasantiago
committed
Multiple loggings channels
Sometimes it's required that we are able to log text to multiple files. Currently the default OpenQA::Setup::app only logs to the destination specified in the config files. With these changes if the developer wants or needs to log to another file/destination, just needs to call the normal logging functions with an extra parameter, which is the name of the "channel". Example: > log_debug($message, 'this is name of channel'); This will work as long as the "channel" already exists. Otherwise, it will use the default logging mechanism. To create a new "channel" just invoke add_log_channel with the **id** as first parameter. The rest of the parameters are the same as the ones required for Mojo::Log. Example: > add_log_channel('channel name', path=>'test.log', level=>'debug') > unless get_channel_handle('autoinst'); This will create a Mojo::Log object that will be associated with the name 'channel name'
1 parent ad74911 commit ec87f0f

File tree

12 files changed

+247
-86
lines changed

12 files changed

+247
-86
lines changed

Diff for: cpanfile

-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ requires 'diagnostics';
9090
requires 'namespace::clean';
9191
requires 'strict';
9292
requires 'warnings';
93-
requires 'File::Path';
9493

9594
on 'test' => sub {
9695
requires 'Perl::Tidy';

Diff for: lib/OpenQA/Setup.pm

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use File::Path 'make_path';
3030

3131
has config => sub { {} };
3232

33-
has log => sub { Mojo::Log->new(handle => \*STDOUT, level => "debug"); };
33+
has log => sub { Mojo::Log->new(handle => \*STDOUT, level => "info"); };
3434

3535
has home => sub { Mojo::Home->new($ENV{MOJO_HOME} || '/') };
3636

Diff for: lib/OpenQA/Utils.pm

+52-10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use Mojo::File 'path';
2525
use IO::Handle;
2626
use Scalar::Util 'blessed';
2727
use Data::Dump 'pp';
28+
use Mojo::Log;
2829

2930
require Exporter;
3031
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
@@ -46,6 +47,9 @@ $VERSION = sprintf "%d.%03d", q$Revision: 1.12 $ =~ /(\d+)/g;
4647
&log_info
4748
&log_error
4849
&log_fatal
50+
add_log_channel
51+
remove_log_channel
52+
get_channel_handle
4953
&save_base64_png
5054
&run_cmd_with_log
5155
&run_cmd_with_log_return_error
@@ -111,6 +115,8 @@ our $imagesdir = "$prjdir/images";
111115
our $hostname = $ENV{SERVER_NAME};
112116
our $app;
113117

118+
my %channels = ();
119+
114120
# the desired new folder structure is
115121
# $testcasedir/<testrepository>
116122
# with "main.pm" and needles being in a productdir under <testrepository>
@@ -219,8 +225,12 @@ sub needle_info {
219225

220226
# logging helpers
221227
sub log_debug {
222-
my ($msg) = @_;
223-
if ($app && $app->log) {
228+
my ($msg, $channel) = @_;
229+
230+
if ($channel && $channels{$channel}) {
231+
$channels{$channel}->debug($msg);
232+
}
233+
elsif ($app && $app->log) {
224234
$app->log->debug($msg);
225235
}
226236
else {
@@ -229,8 +239,11 @@ sub log_debug {
229239
}
230240

231241
sub log_info {
232-
my ($msg) = @_;
233-
if ($app && $app->log) {
242+
my ($msg, $channel) = @_;
243+
if ($channel && $channels{$channel}) {
244+
$channels{$channel}->info($msg);
245+
}
246+
elsif ($app && $app->log) {
234247
$app->log->info($msg);
235248
}
236249
else {
@@ -239,8 +252,11 @@ sub log_info {
239252
}
240253

241254
sub log_warning {
242-
my ($msg) = @_;
243-
if ($app && $app->log) {
255+
my ($msg, $channel) = @_;
256+
if ($channel && $channels{$channel}) {
257+
$channels{$channel}->warn($msg);
258+
}
259+
elsif ($app && $app->log) {
244260
$app->log->warn($msg);
245261
}
246262
else {
@@ -249,8 +265,11 @@ sub log_warning {
249265
}
250266

251267
sub log_error {
252-
my ($msg) = @_;
253-
if ($app && $app->log) {
268+
my ($msg, $channel) = @_;
269+
if ($channel && $channels{$channel}) {
270+
$channels{$channel}->error($msg);
271+
}
272+
elsif ($app && $app->log) {
254273
$app->log->error($msg);
255274
}
256275
else {
@@ -259,8 +278,11 @@ sub log_error {
259278
}
260279

261280
sub log_fatal {
262-
my ($msg) = @_;
263-
if ($app && $app->log) {
281+
my ($msg, $channel) = @_;
282+
if ($channel && $channels{$channel}) {
283+
$channels{$channel}->fatal($msg);
284+
}
285+
elsif ($app && $app->log) {
264286
$app->log->fatal($msg);
265287
}
266288
else {
@@ -269,6 +291,26 @@ sub log_fatal {
269291
die $msg;
270292
}
271293

294+
sub add_log_channel {
295+
my ($channel, @options) = @_;
296+
$channels{$channel} = Mojo::Log->new(@options);
297+
}
298+
299+
sub remove_log_channel {
300+
my ($channel) = @_;
301+
delete $channels{$channel} if $channel;
302+
}
303+
304+
sub get_channel_handle {
305+
my ($channel) = @_;
306+
if ($channel) {
307+
return $channels{$channel}->handle if $channels{$channel};
308+
}
309+
elsif ($app) {
310+
return $app->log->handle;
311+
}
312+
}
313+
272314
sub save_base64_png($$$) {
273315
my ($dir, $newfile, $png) = @_;
274316
return unless $newfile;

Diff for: lib/OpenQA/Worker/Cache.pm

+13-15
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use warnings;
2020
use File::Basename;
2121
use Fcntl ':flock';
2222
use Mojo::UserAgent;
23-
use OpenQA::Utils qw(log_error log_info log_debug);
23+
use OpenQA::Utils qw(log_error log_info log_debug get_channel_handle add_log_channel);
2424
use OpenQA::Worker::Common;
2525
use List::MoreUtils;
2626
use File::Spec::Functions 'catdir';
@@ -82,12 +82,11 @@ sub init {
8282
sub download_asset {
8383
my ($id, $type, $asset, $etag) = @_;
8484

85-
open(my $log, '>>', "autoinst-log.txt") or die("Cannot open autoinst-log.txt");
86-
local $| = 1;
85+
add_log_channel('autoinst', path => 'autoinst-log.txt', level => 'debug') unless get_channel_handle('autoinst');
8786
my $ua = Mojo::UserAgent->new(max_redirects => 2);
8887
$ua->max_response_size(0);
8988
my $url = sprintf '%s/tests/%d/asset/%s/%s', $host, $id, $type, basename($asset);
90-
print $log "Downloading " . basename($asset) . " from $url\n";
89+
log_debug("Downloading " . basename($asset) . " from $url", 'autoinst');
9190
my $tx = $ua->build_tx(GET => $url);
9291
my $headers;
9392

@@ -115,7 +114,7 @@ sub download_asset {
115114
$last_updated = time;
116115
if ($progress < $current) {
117116
$progress = $current;
118-
print $log "CACHE: Downloading $asset: ", $size == $len ? 100 : $progress . "%\n";
117+
log_debug("CACHE: Downloading $asset: ", $size == $len ? 100 : $progress . "%", 'autoinst');
119118
}
120119
}
121120
});
@@ -126,20 +125,20 @@ sub download_asset {
126125
my $size;
127126
if ($code eq 304) {
128127
if (toggle_asset_lock($asset, 0)) {
129-
print $log "CACHE: Content has not changed, not downloading the $asset but updating last use\n";
128+
log_debug("CACHE: Content has not changed, not downloading the $asset but updating last use", 'autoinst');
130129
}
131130
else {
132-
print $log "CACHE: Abnormal situation, code 304. Retrying download\n"; # poo#19270 might give a hint
131+
log_debug("CACHE: Abnormal situation, code 304. Retrying download", 'autoinst');
133132
$asset = 520;
134133
}
135134
}
136135
elsif ($tx->res->is_server_error) {
137136
if (toggle_asset_lock($asset, 0)) {
138-
print $log "CACHE: Could not download the asset, triggering a retry for $code.\n";
137+
log_debug("CACHE: Could not download the asset, triggering a retry for $code.", 'autoinst');
139138
$asset = $code;
140139
}
141140
else {
142-
print $log "CACHE: Abnormal situation, server error. Retrying download\n";
141+
log_debug("CACHE: Abnormal situation, server error. Retrying download", 'autoinst');
143142
$asset = $code;
144143
}
145144
}
@@ -150,19 +149,18 @@ sub download_asset {
150149
if ($size == $headers->content_length) {
151150
check_limits($size);
152151
update_asset($asset, $etag, $size);
153-
print $log "CACHE: Asset download successful to $asset, Cache size is: $cache_real_size\n";
152+
log_debug("CACHE: Asset download successful to $asset, Cache size is: $cache_real_size", 'autoinst');
154153
}
155154
else {
156-
print $log "CACHE: Size of $asset differs, Expected: "
157-
. $headers->content_length
158-
. " / Downloaded: "
159-
. "$size \n";
155+
log_debug(
156+
"CACHE: Size of $asset differs, Expected: " . $headers->content_length . " / Downloaded: " . "$size",
157+
'autoinst');
160158
$asset = 598; # 598 (Informal convention) Network read timeout error
161159
}
162160
}
163161
else {
164162
my $message = $tx->res->error->{message};
165-
print $log "CACHE: Download of $asset failed with: $code - $message\n";
163+
log_debug("CACHE: Download of $asset failed with: $code - $message", 'autoinst');
166164
purge_asset($asset);
167165
$asset = undef;
168166
}

Diff for: lib/OpenQA/Worker/Commands.pm

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package OpenQA::Worker::Commands;
1717
use 5.018;
1818
use warnings;
1919

20-
use OpenQA::Utils qw(log_error log_warning log_debug);
20+
use OpenQA::Utils qw(log_error log_warning log_debug add_log_channel remove_log_channel);
2121
use OpenQA::Worker::Common;
2222
use OpenQA::Worker::Jobs;
2323
use POSIX ':sys_wait_h';
@@ -158,6 +158,8 @@ sub websocket_commands {
158158

159159
if ($job && $job->{id}) {
160160
$OpenQA::Worker::Common::job = $job;
161+
remove_log_channel('autoinst');
162+
add_log_channel('autoinst', path => 'autoinst-log.txt', level => 'debug');
161163
log_debug("Job " . $job->{id} . " scheduled for next cycle");
162164
Mojo::IOLoop->singleton->once(
163165
"start_job" => sub {

Diff for: lib/OpenQA/Worker/Engines/isotovideo.pm

+14-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use strict;
1818
use warnings;
1919

2020
use OpenQA::Worker::Common;
21-
use OpenQA::Utils qw(locate_asset log_error log_info log_debug);
21+
use OpenQA::Utils qw(locate_asset log_error log_info log_debug get_channel_handle);
2222

2323
use POSIX qw(:sys_wait_h strftime uname _exit);
2424
use JSON 'to_json';
@@ -29,6 +29,7 @@ use Errno;
2929
use Cwd qw(abs_path getcwd);
3030
use OpenQA::Worker::Cache;
3131
use Time::HiRes 'sleep';
32+
use IO::Handle;
3233

3334
my $isotovideo = "/usr/bin/isotovideo";
3435
my $workerpid;
@@ -119,13 +120,10 @@ sub detect_asset_keys {
119120
sub engine_workit {
120121
my ($job) = @_;
121122

122-
if (open(my $log, '>', "autoinst-log.txt")) {
123-
print $log "+++ setup notes +++\n";
124-
printf $log "start time: %s\n", strftime("%F %T", gmtime);
125-
my ($sysname, $hostname, $release, $version, $machine) = POSIX::uname();
126-
printf $log "running on $hostname:%d ($sysname $release $version $machine)\n", $instance;
127-
close($log);
128-
}
123+
my ($sysname, $hostname, $release, $version, $machine) = POSIX::uname();
124+
log_debug('+++ setup notes +++', 'autoinst');
125+
log_debug(sprintf("start time: %s", strftime("%F %T", gmtime)), 'autoinst');
126+
log_debug(sprintf("running on $hostname:%d ($sysname $release $version $machine)", $instance), 'autoinst');
129127

130128
# set base dir to the one assigned with webui
131129
OpenQA::Utils::change_sharedir($hosts->{$current_host}{dir});
@@ -213,15 +211,14 @@ sub engine_workit {
213211
setpgrp(0, 0);
214212
$ENV{TMPDIR} = $tmpdir;
215213
log_info("$$: WORKING " . $job->{id});
216-
if (open(my $log, '>>', "autoinst-log.txt")) {
217-
print $log "+++ worker notes +++\n";
218-
printf $log "start time: %s\n", strftime("%F %T", gmtime);
219-
my ($sysname, $hostname, $release, $version, $machine) = POSIX::uname();
220-
printf $log "running on $hostname:%d ($sysname $release $version $machine)\n", $instance;
221-
close($log);
222-
}
223-
open STDOUT, ">>", "autoinst-log.txt";
224-
open STDERR, ">&STDOUT";
214+
log_debug('+++ worker notes +++', 'autoinst');
215+
log_debug(sprintf("start time: %s", strftime("%F %T", gmtime)), 'autoinst');
216+
217+
my ($sysname, $hostname, $release, $version, $machine) = POSIX::uname();
218+
log_debug(sprintf("running on $hostname:%d ($sysname $release $version $machine)", $instance), 'autoinst');
219+
my $handle = get_channel_handle('autoinst');
220+
STDOUT->fdopen($handle, 'w');
221+
STDERR->fdopen($handle, 'w');
225222
exec "perl", "$isotovideo", '-d';
226223
die "exec failed: $!\n";
227224
}

Diff for: lib/OpenQA/Worker/Jobs.pm

+10-22
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ sub check_job {
126126

127127
sub stop_job {
128128
my ($aborted, $job_id) = @_;
129-
130129
# we call this function in all situations, so better check
131130
if (!$job || $stop_job_running) {
132131
# In case there is no job, or if the job was asked to stop
@@ -188,9 +187,7 @@ sub upload {
188187

189188
# we need to open and close the log here as one of the files
190189
# might actually be autoinst-log.txt
191-
open(my $log, '>>', "autoinst-log.txt");
192-
printf $log "uploading %s\n", $filename;
193-
close $log;
190+
log_debug("uploading $filename", 'autoinst');
194191
log_debug("uploading $filename");
195192

196193
my $regular_upload_failed = 0;
@@ -228,10 +225,8 @@ sub upload {
228225

229226
# Just return if all upload retries have failed
230227
# this will cause the next group of uploads to be triggered
231-
my $msg = "All $retry_limit upload attempts have failed for $filename\n";
232-
open(my $log, '>>', "autoinst-log.txt");
233-
print $log $msg;
234-
close $log;
228+
my $msg = "All $retry_limit upload attempts have failed for $filename";
229+
log_debug($msg, 'autoinst');
235230
log_error($msg);
236231
return 0;
237232
}
@@ -246,9 +241,7 @@ sub upload {
246241
else {
247242
$msg = sprintf "ERROR %s: Connection error: $err->{message}\n", $filename;
248243
}
249-
open(my $log, '>>', "autoinst-log.txt");
250-
print $log $msg;
251-
close $log;
244+
log_debug($msg, 'autoinst');
252245
log_error($msg);
253246
return 0;
254247
}
@@ -267,15 +260,15 @@ sub upload {
267260
($csum2, $size2) = split(/ /, <$cfd>);
268261
close($cfd);
269262
}
270-
open(my $log, '>>', "autoinst-log.txt");
271-
print $log "Checksum comparison (actual:expected) $csum1:$csum2 with size (actual:expected) $size1:$size2\n";
263+
log_debug("Checksum comparison (actual:expected) $csum1:$csum2 with size (actual:expected) $size1:$size2",
264+
'autoinst');
272265
if ($csum1 eq $csum2 && $size1 eq $size2) {
273266
my $ua_url = $hosts->{$current_host}{url}->clone;
274267
$ua_url->path("jobs/$job_id/ack_temporary");
275268
$hosts->{$current_host}{ua}->post($ua_url => form => {temporary => $res->res->json->{temporary}});
276269
}
277270
else {
278-
print $log "Checksum/size comparison of $filename FAILED\n";
271+
log_debug("Checksum/size comparison of $filename FAILED", 'autoinst');
279272
return 0;
280273
}
281274
}
@@ -318,13 +311,9 @@ sub _stop_job_2 {
318311
$aborted ||= 'done';
319312

320313
my $job_done; # undef
321-
322-
open(my $log, '>>', "autoinst-log.txt");
323-
print $log "+++ worker notes +++\n";
324-
printf $log "end time: %s\n", strftime("%F %T", gmtime);
325-
print $log "result: $aborted\n";
326-
close $log;
327-
314+
log_debug("+++ worker notes +++", 'autoinst');
315+
log_debug(sprintf("end time: %s", strftime("%F %T", gmtime)), 'autoinst');
316+
log_debug("result: $aborted", 'autoinst');
328317
if ($aborted ne 'quit' && $aborted ne 'abort' && $aborted ne 'api-failure') {
329318
# collect uploaded logs
330319
my @uploaded_logfiles = glob "$pooldir/ulogs/*";
@@ -367,7 +356,6 @@ sub _stop_job_2 {
367356
}
368357
}
369358
}
370-
371359
for my $file (qw(video.ogv vars.json serial0 autoinst-log.txt virtio_console.log)) {
372360
next unless -e $file;
373361
# default serial output file called serial0

Diff for: t/24-worker.t

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ ok($hosts->{this_host_should_not_exist}{url}, 'url object created');
6363
is($hosts->{this_host_should_not_exist}{workerid}, undef, 'worker not registered yet');
6464

6565
# api_call
66+
print "BEFORE:\n";
6667
eval { OpenQA::Worker::Common::api_call() };
6768
ok($@, 'no action or no worker id set');
6869

0 commit comments

Comments
 (0)