Skip to content

Commit 31ec90c

Browse files
authored
Merge pull request os-autoinst#1480 from dasantiago/25958
Log Uniformization
2 parents 9575846 + 4f54690 commit 31ec90c

21 files changed

+489
-263
lines changed

Diff for: lib/OpenQA/FakeApp.pm

-72
This file was deleted.

Diff for: lib/OpenQA/ResourceAllocator.pm

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ use Scalar::Util 'blessed';
2525

2626
use OpenQA::IPC;
2727
use OpenQA::Utils qw(log_debug wakeup_scheduler exists_worker safe_call);
28-
use OpenQA::ServerStartup;
2928
use OpenQA::Resource::Jobs ();
3029
use OpenQA::Resource::Locks ();
31-
use OpenQA::FakeApp;
30+
use OpenQA::Setup;
3231
use sigtrap handler => \&normal_signals_handler, 'normal-signals';
3332

3433
my $singleton;
@@ -59,9 +58,9 @@ sub new {
5958

6059
sub run {
6160
my $self = shift;
62-
my $fakeapp = OpenQA::FakeApp->new(log_name => 'resource-allocator');
63-
OpenQA::ServerStartup::read_config($fakeapp);
64-
OpenQA::ServerStartup::setup_logging($fakeapp);
61+
my $setup = OpenQA::Setup->new(log_name => 'resource-allocator');
62+
OpenQA::Setup::read_config($setup);
63+
OpenQA::Setup::setup_log($setup);
6564
log_debug("Resource allocator started");
6665
$self->{reactor}->run()
6766
if exists $self->{reactor} && blessed($self->{reactor}) && $self->{reactor}->isa("Net::DBus::Reactor");

Diff for: lib/OpenQA/Scheduler.pm

+5-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ use Net::DBus::Reactor;
2323
use Data::Dump 'pp';
2424

2525
use OpenQA::IPC;
26-
use OpenQA::FakeApp;
26+
use OpenQA::Setup;
2727

2828
use OpenQA::Utils 'log_debug';
29-
use OpenQA::ServerStartup;
3029

3130
# How many jobs to allocate in one tick. Defaults to 50 ( set it to 0 for as much as possible)
3231
use constant MAX_JOB_ALLOCATION => $ENV{OPENQA_SCHEDULER_MAX_JOB_ALLOCATION} // 50;
@@ -75,11 +74,11 @@ sub _is_method_allowed {
7574
return $ret;
7675
}
7776

78-
our $fakeapp;
77+
our $setup;
7978
sub run {
80-
$fakeapp = OpenQA::FakeApp->new;
81-
OpenQA::ServerStartup::read_config($fakeapp);
82-
OpenQA::ServerStartup::setup_logging($fakeapp);
79+
$setup = OpenQA::Setup->new(log_name => 'scheduler');
80+
OpenQA::Setup::read_config($setup);
81+
OpenQA::Setup::setup_log($setup);
8382

8483
OpenQA::Scheduler->new();
8584
log_debug("Scheduler started");

Diff for: lib/OpenQA/ServerStartup.pm renamed to lib/OpenQA/Setup.pm

+104-42
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2015-2016 SUSE LLC
1+
# Copyright (C) 2017 SUSE LLC
22
#
33
# This program is free software; you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -13,20 +13,117 @@
1313
# You should have received a copy of the GNU General Public License along
1414
# with this program; if not, see <http://www.gnu.org/licenses/>.
1515

16-
# This package contains shared functions between WebAPI and WebSockets
17-
package OpenQA::ServerStartup;
18-
16+
package OpenQA::Setup;
17+
use Mojo::Log;
18+
use Mojo::Home;
1919
use strict;
2020
use warnings;
21+
use Mojo::Base -base;
22+
use Sys::Hostname;
23+
use File::Spec::Functions 'catfile';
24+
use Mojo::File 'path';
2125
use Config::IniFiles;
2226
use db_profiler;
2327
use db_helpers;
2428
use OpenQA::Utils;
25-
use Mojo::File 'path';
29+
use File::Path 'make_path';
2630

27-
sub read_config {
28-
my $app = shift;
31+
has config => sub { {} };
32+
33+
has log => sub { Mojo::Log->new(handle => \*STDOUT, level => "info"); };
34+
35+
has home => sub { Mojo::Home->new($ENV{MOJO_HOME} || '/') };
36+
37+
has mode => 'production';
38+
39+
has 'log_name';
40+
41+
has level => 'info';
42+
43+
has 'instance';
44+
45+
has 'log_dir';
46+
47+
has schema => sub { OpenQA::Schema::connect_db() };
48+
49+
sub setup_log {
50+
my ($self) = @_;
51+
my ($logfile, $logdir, $level, $log);
52+
53+
if ($self->isa('OpenQA::Setup')) {
54+
$logdir = $self->log_dir;
55+
$level = $self->level;
56+
if ($logdir && !-e $logdir) {
57+
make_path($logdir);
58+
}
59+
elsif ($logdir && !-d $logdir) {
60+
die "Please point the logs to a valid folder!";
61+
}
62+
}
63+
else {
64+
$log = $self->log;
65+
$level = $self->config->{logging}->{level} || 'debug';
66+
}
67+
$logfile = $ENV{OPENQA_LOGFILE} || $self->config->{logging}->{file};
68+
69+
if ($logfile && $logdir) {
70+
$logfile = catfile($logdir, $logfile);
71+
$log = Mojo::Log->new(
72+
handle => path($logfile)->open('>>'),
73+
level => $self->level,
74+
format => sub { return log_format($self->log_name, @_); });
75+
}
76+
elsif ($logfile) {
77+
$log = Mojo::Log->new(
78+
handle => path($logfile)->open('>>'),
79+
level => $level,
80+
format => sub { return log_format($self->log_name, @_); });
81+
}
82+
elsif ($logdir) {
83+
# So each worker from each host get it's own log (as the folder can be shared). Hopefully the machine hostname
84+
# is already sanitized. Otherwise we need to check
85+
$logfile
86+
= catfile($logdir, hostname() . (defined $self->instance ? "-${\$self->instance}" : '') . ".log");
87+
$log = Mojo::Log->new(
88+
handle => path($logfile)->open('>>'),
89+
level => $self->level,
90+
format => sub { return log_format($self->log_name, @_); });
91+
}
92+
else {
93+
$log = Mojo::Log->new(
94+
handle => \*STDOUT,
95+
level => $level,
96+
format => sub {
97+
my ($time, $level, @lines) = @_;
98+
return "[${\$self->log_name}:$level] " . join "\n", @lines, '';
99+
});
100+
}
101+
102+
$self->log($log);
103+
unless ($self->isa('OpenQA::Setup')) {
104+
if ($ENV{OPENQA_SQL_DEBUG} // $self->config->{logging}->{sql_debug} // 'false' eq 'true') {
105+
# avoid enabling the SQL debug unless we really want to see it
106+
# it's rather expensive
107+
db_profiler::enable_sql_debugging($self);
108+
}
109+
}
29110

111+
$OpenQA::Utils::app = $self;
112+
return $log;
113+
}
114+
115+
sub log_format {
116+
my ($logname, $time, $level, @lines) = @_;
117+
return '[' . localtime($time) . "] [$logname:$level] " . join "\n", @lines, '';
118+
}
119+
120+
sub emit_event {
121+
my ($self, $event, $data) = @_;
122+
# nothing to see here, move along
123+
}
124+
125+
sub read_config {
126+
my $app = shift;
30127
my %defaults = (
31128
global => {
32129
appname => 'openQA',
@@ -123,41 +220,6 @@ sub read_config {
123220
$app->config->{auth}->{method} =~ s/\s//g;
124221
}
125222

126-
sub setup_logging {
127-
my ($app) = @_;
128-
129-
my $config = $app->config;
130-
my $logfile = $ENV{OPENQA_LOGFILE} || $config->{logging}->{file};
131-
132-
if ($logfile) {
133-
$app->log->handle(path($logfile)->open('>>'));
134-
$app->log->path($logfile);
135-
$app->log->format(
136-
sub {
137-
my ($time, $level, @lines) = @_;
138-
return '[' . localtime($time) . "] [" . $app->log_name . ":$level] " . join "\n", @lines, '';
139-
});
140-
}
141-
else {
142-
$app->log->format(
143-
sub {
144-
my (undef, $level, @lines) = @_;
145-
return '[' . $app->log_name . ":$level] " . join "\n", @lines, '';
146-
});
147-
}
148-
149-
if ($config->{logging}->{level}) {
150-
$app->log->level($config->{logging}->{level});
151-
}
152-
if ($ENV{OPENQA_SQL_DEBUG} // $config->{logging}->{sql_debug} // 'false' eq 'true') {
153-
# avoid enabling the SQL debug unless we really want to see it
154-
# it's rather expensive
155-
db_profiler::enable_sql_debugging($app);
156-
}
157-
158-
$OpenQA::Utils::app = $app;
159-
}
160-
161223
# Update config definition from plugin requests
162224
sub update_config {
163225
my ($config, @namespaces) = @_;

0 commit comments

Comments
 (0)