MT#7705 - Rework logging for Controller/Billing module. Improve Message.pm $log parsing and shown as LOG= (optional)

agranig/webphone
Kirill Solomko 11 years ago
parent f759cbeb98
commit 68a1aa349e

@ -71,7 +71,11 @@ sub base :Chained('profile_list') :PathPart('') :CaptureArgs(1) {
my ($self, $c, $profile_id) = @_;
unless($profile_id && $profile_id->is_integer) {
$c->flash(messages => [{type => 'error', text => $c->loc('Invalid profile id detected!')}]);
NGCP::Panel::Utils::Message->error(
c => $c,
data => { id => $profile_id },
desc => $c->loc('Invalid profile id detected!'),
);
$c->response->redirect($c->uri_for());
return;
}
@ -84,7 +88,11 @@ sub base :Chained('profile_list') :PathPart('') :CaptureArgs(1) {
my $res = $c->stash->{profiles_rs}->find($profile_id);
unless(defined($res)) {
$c->flash(messages => [{type => 'error', text => $c->loc('Billing Profile does not exist!')}]);
NGCP::Panel::Utils::Message->error(
c => $c,
data => { id => $profile_id },
desc => $c->loc('Billing Profile does not exist!'),
);
$c->response->redirect($c->uri_for());
return;
}
@ -178,12 +186,15 @@ sub edit :Chained('base') :PathPart('edit') {
});
delete $c->session->{created_objects}->{reseller};
$c->flash(messages => [{type => 'success', text => $c->loc('Billing profile successfully updated')}]);
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $c->loc('Billing profile successfully updated'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc("Failed to update billing profile."),
desc => $c->loc('Failed to update billing profile'),
);
}
NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/billing'));
@ -233,13 +244,15 @@ sub create :Chained('profile_list') :PathPart('create') :Args(0) {
my $profile = $c->model('DB')->resultset('billing_profiles')->create($form->values);
$c->session->{created_objects}->{billing_profile} = { id => $profile->id };
delete $c->session->{created_objects}->{reseller};
$c->flash(messages => [{type => 'success', text => $c->loc('Billing profile successfully created')}]);
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $c->loc('Billing profile successfully created'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc("Failed to create billing profile."),
desc => $c->loc('Failed to create billing profile'),
);
}
NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/billing'));
@ -277,7 +290,13 @@ sub fees_base :Chained('fees_list') :PathPart('') :CaptureArgs(1) {
my ($self, $c, $fee_id) = @_;
unless($fee_id && $fee_id->is_integer) {
$c->flash(messages => [{type => 'error', text => $c->loc('Invalid billing fee id detected!')}]);
$fee_id ||= '';
NGCP::Panel::Utils::Message->error(
c => $c,
data => $c->stash->{profile},
log => $fee_id,
desc => $c->loc('Invalid billing fee id detected!'),
);
$c->response->redirect($c->uri_for($c->stash->{profile}->{id}, 'fees'));
return;
}
@ -286,7 +305,12 @@ sub fees_base :Chained('fees_list') :PathPart('') :CaptureArgs(1) {
->search(undef, {join => 'billing_zone',})
->find($fee_id);
unless(defined($res)) {
$c->flash(messages => [{type => 'error', text => $c->loc('Billing Fee does not exist!')}]);
NGCP::Panel::Utils::Message->error(
c => $c,
data => $c->stash->{profile},
log => $fee_id,
desc => $c->loc('Billing Fee does not exist!'),
);
$c->response->redirect($c->uri_for($c->stash->{profile}->{id}, 'fees'));
return;
}
@ -327,8 +351,10 @@ sub fees_create :Chained('fees_list') :PathPart('create') :Args(0) {
$c->stash->{'profile_result'}
->billing_fees->create($form->values);
delete $c->session->{created_objects}->{billing_zone};
$c->flash(messages => [{type => 'success', text => $c->loc('Billing Fee successfully created!')}]);
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $c->loc('Billing Fee successfully created!'),
);
$c->response->redirect($c->uri_for($c->stash->{profile}->{id}, 'fees'));
return;
}
@ -355,7 +381,10 @@ sub fees_upload :Chained('fees_list') :PathPart('upload') :Args(0) {
# TODO: check by formhandler?
unless($upload) {
$c->flash(messages => [{type => 'error', text => $c->loc('No Billing Fee file specified!')}]);
NGCP::Panel::Utils::Message->error(
c => $c,
desc => $c->loc('No Billing Fee file specified!'),
);
$c->response->redirect($c->uri_for($c->stash->{profile}->{id}, 'fees'));
return;
}
@ -406,11 +435,16 @@ sub fees_upload :Chained('fees_list') :PathPart('upload') :Args(0) {
if(@fails) {
$text .= $c->loc(", but skipped the following line numbers: ") . (join ", ", @fails);
}
$c->flash(messages => [{type => 'success', text => $text}]);
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $text,
);
} catch($e) {
$c->log->error("failed to upload csv: $e");
$c->flash(messages => [{type => 'error', text => $c->loc('Failed to upload Billing Fees')}]);
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc('Failed to upload Billing Fees'),
);
};
$c->response->redirect($c->uri_for($c->stash->{profile}->{id}, 'fees'));
@ -448,7 +482,10 @@ sub fees_edit :Chained('fees_base') :PathPart('edit') :Args(0) {
$c->stash->{'fee_result'}
->update($form->values);
delete $c->session->{created_objects}->{billing_zone};
$c->flash(messages => [{type => 'success', text => $c->loc('Billing Profile successfully changed!')}]);
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $c->loc('Billing fee successfully changed!'),
);
$c->response->redirect($c->uri_for($c->stash->{profile}->{id}, 'fees'));
return;
}
@ -461,12 +498,20 @@ sub fees_delete :Chained('fees_base') :PathPart('delete') :Args(0) {
my ($self, $c) = @_;
unless ( defined($c->stash->{'fee_result'}) ) {
$c->flash(messages => [{type => 'error', text => $c->loc('Billing fee not found!')}]);
NGCP::Panel::Utils::Message->error(
c => $c,
data => $c->stash->{profile},
desc => $c->loc('Billing fee not found!'),
);
return;
}
$c->stash->{'fee_result'}->delete;
$c->flash(messages => [{type => 'success', text => $c->loc('Billing profile successfully deleted!')}]);
NGCP::Panel::Utils::Message->info(
c => $c,
data => $c->stash->{profile},
desc => $c->loc('Billing fee successfully deleted!'),
);
$c->response->redirect($c->uri_for($c->stash->{profile}->{id}, 'fees'));
}
@ -507,12 +552,15 @@ sub zones_create :Chained('zones_list') :PathPart('create') :Args(0) {
try {
my $zone = $c->stash->{'profile_result'}->billing_zones->create($form->values);
$c->session->{created_objects}->{billing_zone} = { id => $zone->id };
$c->flash(messages => [{type => 'success', text => $c->loc('Billing Zone successfully created')}]);
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $c->loc('Billing Zone successfully created'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc('Failed to create billing zone.'),
desc => $c->loc('Failed to create billing zone'),
);
}
NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{zones_root_uri});
@ -529,14 +577,25 @@ sub zones_base :Chained('zones_list') :PathPart('') :CaptureArgs(1) {
my ($self, $c, $zone_id) = @_;
unless($zone_id && $zone_id->is_integer) {
$c->flash(messages => [{type => 'error', text => $c->loc('Invalid billing zone id detected')}]);
$zone_id ||= '';
NGCP::Panel::Utils::Message->error(
c => $c,
data => $c->stash->{profile},
log => $zone_id,
desc => $c->loc('Invalid billing zone id detected'),
);
NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{zones_root_uri});
}
my $res = $c->stash->{'profile_result'}->billing_zones
->find($zone_id);
unless(defined($res)) {
$c->flash(messages => [{type => 'error', text => $c->loc('Billing zone does not exist!')}]);
NGCP::Panel::Utils::Message->error(
c => $c,
data => $c->stash->{profile},
log => $zone_id,
desc => $c->loc('Billing zone does not exist!'),
);
NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{zones_root_uri});
}
$c->stash(zone_result => $res);
@ -545,14 +604,20 @@ sub zones_base :Chained('zones_list') :PathPart('') :CaptureArgs(1) {
sub zones_delete :Chained('zones_base') :PathPart('delete') :Args(0) {
my ($self, $c) = @_;
my $zone_info = { $c->stash->{zone_result}->get_inflated_columns };
try {
$c->stash->{zone_result}->delete;
$c->flash(messages => [{type => 'success', text => $c->loc('Billing zone successfully deleted')}]);
NGCP::Panel::Utils::Message->info(
c => $c,
data => $zone_info,
desc => $c->loc('Billing zone successfully deleted'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc('Failed to delete billing zone.'),
data => $zone_info,
desc => $c->loc('Failed to delete billing zone'),
);
}
NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{zones_root_uri});
@ -585,10 +650,13 @@ sub peaktimes :Chained('peaktimes_list') :PathPart('') :Args(0) {
sub peaktime_weekdays_base :Chained('peaktimes_list') :PathPart('weekday') :CaptureArgs(1) {
my ($self, $c, $weekday_id) = @_;
unless (defined $weekday_id && $weekday_id >= 0 && $weekday_id <= 6) {
$c->flash(messages => [{
type => 'error',
text => $c->loc('This weekday does not exist.')
}]);
$weekday_id ||= '';
NGCP::Panel::Utils::Message->error(
c => $c,
data => $c->stash->{profile},
log => $weekday_id,
desc => $c->loc('This weekday does not exist'),
);
$c->response->redirect($c->uri_for_action(
"/billing/peaktimes", [$c->req->captures->[0]],
));
@ -610,23 +678,34 @@ sub peaktime_weekdays_edit :Chained('peaktime_weekdays_base') :PathPart('edit')
$form->values->{end} = '23:59:59' unless($form->values->{end});
$c->stash->{'weekdays_result'}
->create($form->values);
NGCP::Panel::Utils::Message->info(
c => $c,
data => { %{$c->request->params},
billing_profile_id => $c->stash->{profile}{id}, },
desc => $c->loc('Timerange has been successfully created'),
);
}
my $delete_param = $c->request->params->{delete};
if($delete_param) {
my $rs = $c->stash->{weekdays_result}
->find($delete_param);
unless ($rs) {
$c->flash(messages => [{
type => 'error',
text => $c->loc('The timerange you wanted to delete does not exist.')
}]);
NGCP::Panel::Utils::Message->error(
c => $c,
desc => $c->loc('The timerange you wanted to delete does not exist'),
);
$c->response->redirect($c->uri_for_action(
"/billing/peaktimes", [$c->req->captures->[0]],
));
return;
}
$rs->delete();
NGCP::Panel::Utils::Message->info(
c => $c,
data => { $rs->get_inflated_columns },
desc => $c->loc('Timerange has been successfully deleted'),
);
}
$form = NGCP::Panel::Form::BillingPeaktimeWeekdays->new
@ -683,8 +762,12 @@ sub peaktime_specials_ajax :Chained('peaktimes_list') :PathPart('ajax') :Args(0)
sub peaktime_specials_base :Chained('peaktimes_list') :PathPart('date') :CaptureArgs(1) {
my ($self, $c, $special_id) = @_;
unless($special_id && $special_id->is_integer) {
$c->flash(messages => [{type => 'error', text => $c->loc('Invalid peaktime date id detected!')}]);
unless($special_id && $special_id->is_integer) {
$special_id ||= '';
NGCP::Panel::Utils::Message->error(
c => $c,
desc => $c->loc('Invalid peaktime date id detected!'),
);
$c->response->redirect($c->stash->{peaktimes_root_uri});
return;
}
@ -692,7 +775,12 @@ sub peaktime_specials_base :Chained('peaktimes_list') :PathPart('date') :Capture
my $res = $c->stash->{'profile_result'}->billing_peaktime_specials
->find($special_id);
unless(defined($res)) {
$c->flash(messages => [{type => 'error', text => $c->loc('Peaktime date does not exist!')}]);
NGCP::Panel::Utils::Message->error(
c => $c,
data => $c->stash->{profile},
log => $special_id,
desc => $c->loc('Peaktime date does not exist!'),
);
$c->response->redirect($c->stash->{peaktimes_root_uri});
return;
}
@ -721,12 +809,19 @@ sub peaktime_specials_edit :Chained('peaktime_specials_base') :PathPart('edit')
if($posted && $form->validated) {
try {
$c->stash->{special_result}->update($form->values);
$c->flash(messages => [{type => 'success', text => $c->loc('Special offpeak entry successfully updated')}]);
NGCP::Panel::Utils::Message->info(
c => $c,
data => { %{$c->request->params},
billing_profile_id => $c->stash->{profile}{id}, },
desc => $c->loc('Special offpeak entry successfully updated'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc('Failed to update special offpeak entry.'),
data => { %{$c->request->params},
billing_profile_id => $c->stash->{profile}{id}, },
desc => $c->loc('Failed to update special offpeak entry'),
);
}
NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{peaktimes_root_uri});
@ -738,14 +833,20 @@ sub peaktime_specials_edit :Chained('peaktime_specials_base') :PathPart('edit')
sub peaktime_specials_delete :Chained('peaktime_specials_base') :PathPart('delete') :Args(0) {
my ($self, $c) = @_;
my $special_result_info = { $c->stash->{special_result}->get_inflated_columns };
try {
$c->stash->{special_result}->delete;
$c->flash(messages => [{type => 'success', text => 'Special offpeak entry successfully deleted'}]);
NGCP::Panel::Utils::Message->info(
c => $c,
data => $special_result_info,
desc => $c->loc('Special offpeak entry successfully deleted'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc('Failed to delete special offpeak entry.'),
data => $special_result_info,
desc => $c->loc('Failed to delete special offpeak entry'),
);
}
NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{peaktimes_root_uri});
@ -773,12 +874,19 @@ sub peaktime_specials_create :Chained('peaktimes_list') :PathPart('date/create')
try {
$c->stash->{'profile_result'}->billing_peaktime_specials
->create($form->values);
$c->flash(messages => [{type => 'success', text => $c->loc('Special offpeak entry successfully created')}]);
NGCP::Panel::Utils::Message->info(
c => $c,
data => { %{$c->request->params},
billing_profile_id => $c->stash->{profile}{id}, },
desc => $c->loc('Special offpeak entry successfully created'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc('Failed to create special offpeak entry.'),
data => { %{$c->request->params},
billing_profile_id => $c->stash->{profile}{id}, },
desc => $c->loc('Failed to create special offpeak entry'),
);
}
NGCP::Panel::Utils::Navigation::back_or($c, $c->stash->{peaktimes_root_uri});

@ -72,6 +72,10 @@ method get_log_params ($self: Catalyst :$c, :$type?, :$data?) {
} else {
$data_str = '';
}
if (length($data_str) > 100000) {
# trim long messages
$data_str = "{ data => 'Msg size is too big' }";
}
unless ($c->config->{logging}->{clear_passwords}) {
}
@ -85,7 +89,7 @@ method get_log_params ($self: Catalyst :$c, :$type?, :$data?) {
};
}
method error ($self: Catalyst :$c, Str :$desc, Str :$log?, :$error?, :$type = 'panel', :$data?) {
method error ($self: Catalyst :$c, Str :$desc, :$log?, :$error?, :$type = 'panel', :$data?) {
# we explicitly declare the invocant to skip the validation for Object
# because we want a class method instead of an object method
@ -93,26 +97,29 @@ method error ($self: Catalyst :$c, Str :$desc, Str :$log?, :$error?, :$type = 'p
type => $type,
data => $data, );
my $log_msg = '';
my $msg = ''; # sent to the log file
my $log_msg = ''; # optional log info
my $usr_type = 'error';
my $usr_text = $desc;
given (1) {
when (defined $log)
{
if (ref($log)) {
$log_msg = Data::Dumper->new([ $log ])
->Terse(1)
->Dump;
} else {
$log_msg = $log
}
$log_msg =~ s/\n//g;
$log_msg =~ s/\s+/ /g;
if (defined $log)
{
if (ref($log)) {
$log_msg = Data::Dumper->new([ $log ])
->Terse(1)
->Dump;
} else {
$log_msg = $log
}
$log_msg =~ s/\n//g;
$log_msg =~ s/\s+/ /g;
$log_msg and $log_msg = "LOG=$log_msg";
}
given (1) {
when (not defined $error)
{
$log_msg = $desc;
$msg = $desc;
}
when (my ($host) = $error =~ /problem connecting to (\S+, port [0-9]+)/ )
{
@ -121,31 +128,31 @@ method error ($self: Catalyst :$c, Str :$desc, Str :$log?, :$error?, :$type = 'p
}
when (ref($error) eq "ARRAY" && @$error >= 2 && $error->[1] eq "showdetails" )
{
$log_msg = "$desc (@$error[0])";
$msg = "$desc (@$error[0])";
$usr_text = "$desc (@$error[0])";
}
when (not $error->isa('DBIx::Class::Exception') )
{
$log_msg = "$desc ($error)";
$msg = "$desc ($error)";
$usr_text = $desc;
}
when (my ($dup) = $error =~ /(Duplicate entry \S*)/ )
{
$log_msg = "$desc ($error)";
$msg = "$desc ($error)";
$usr_text = "$desc ($dup)";
}
when (my ($excerpt) = $error =~ /(Column \S+ cannot be null)/ ) {
$log_msg = "$desc ($error)";
$msg = "$desc ($error)";
$usr_text = "$desc ($excerpt)";
}
default {
$log_msg = "$desc ($error)";
$msg = "$desc ($error)";
}
}
my $rc = $c->log->error(
sprintf <<EOF, @{$log_params}{qw(r_ip called tx_id r_user data)});
IP=%s CALLED=%s TX=%s USER=%s DATA=%s MSG=$log_msg
IP=%s CALLED=%s TX=%s USER=%s DATA=%s MSG="$msg" $log_msg
EOF
if ($type eq 'panel') {
$c->flash(messages => [{ type => $usr_type,
@ -157,7 +164,7 @@ EOF
return $rc;
}
method info ($self: Catalyst :$c, Str :$desc, Str :$log?, :$type = 'panel', :$data?) {
method info ($self: Catalyst :$c, Str :$desc, :$log?, :$type = 'panel', :$data?) {
# we explicitly declare the invocant to skip the validation for Object
# because we want a class method instead of an object method
@ -165,32 +172,27 @@ method info ($self: Catalyst :$c, Str :$desc, Str :$log?, :$type = 'panel', :$da
type => $type,
data => $data, );
my $log_msg = '';
my $msg = $desc; # sent to the log file
my $log_msg = ''; # optional log info
my $usr_type = 'info';
my $usr_text = $desc;
given (1) {
when (defined $log)
{
if (ref($log)) {
$log_msg = Data::Dumper->new([ $log ])
->Terse(1)
->Dump;
} else {
$log_msg = $log
}
$log_msg =~ s/\n//g;
$log_msg =~ s/\s+/ /g;
}
default
{
$log_msg = $desc;
if (defined $log) {
if (ref($log)) {
$log_msg = Data::Dumper->new([ $log ])
->Terse(1)
->Dump;
} else {
$log_msg = $log
}
$log_msg =~ s/\n//g;
$log_msg =~ s/\s+/ /g;
$log_msg and $log_msg = "LOG=$log_msg";
}
my $rc = $c->log->info(
sprintf <<EOF, @{$log_params}{qw(r_ip called tx_id r_user data)});
IP=%s CALLED=%s TX=%s USER=%s DATA=%s MSG=$log_msg
IP=%s CALLED=%s TX=%s USER=%s DATA=%s MSG="$msg" $log_msg
EOF
if ($type eq 'panel') {
$c->flash(messages => [{ type => $usr_type, text => $usr_text }]);

Loading…
Cancel
Save