in reply to Logging to web page with Mojolicious and strict

Maybe declare our $logLine; in your main perl script (index.mojo) and then access the variable in each of the files you require with its fully qualified name which will be $main::logLine ?

However, if it was me, I would create a logging class and instantiate a global log object. Which will have api like log("hello") and toString() and toHTML(). In this way I expand my logging capabilities as much as I like with just one single global variable (the log object). And when I feel like, I modify all my subs to take just one additional parameter which will be the log object. No matter how I modify the log object in the future, I will not need a second global variable and I will not need to modify my subs for a second time...

bw, bliako

Replies are listed 'Best First'.
Re^2: Logging to web page with Mojolicious and strict
by TieUpYourCamel (Scribe) on Sep 13, 2019 at 19:56 UTC
    Thanks for your suggestion. I've written a Logger package:
    package Logger; use Mojo::Log; sub new { my $class = shift; my $self = {$LogLine = '', $app = shift}; bless $self, $class; return $self; } sub add { my ( $self, $LogItem ) = @_; my $time = FormatDate(); $self->{$LogLine} .= "[ $time ] :: $LogItem\n" if defined($LogItem +); $app->log->debug($LogItem); } sub get { my( $self ) = @_; return $self->{$LogLine}; } sub clear { my( $self ) = @_; $self->{$LogLine} = ''; } sub FormatDate { my $Offset = shift; $Offset = 0 unless ($Offset); my ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOf +Year, $IsDST) = localtime(time+$Offset); $Year = $Year + 1900; $Month = $Month + 1; $Day = sprintf ("%02s", $Day); $Month = sprintf ("%02s", $Month); $Hour = sprintf ("%02s", $Hour); $Minute = sprintf ("%02s", $Minute); $Second = sprintf ("%02s", $Second); return $Year."-".$Month."-".$Day." ".$Hour.":".$Minute.":".$Second +; } 1;
    and it's working very well.

      Thanks for sharing your code.

      Does it still work when you add use strict; use warnings; ?

      From my point I see 2 problems in this line {$LogLine = '', $app = shift}; (and similar lines further on). Inserting key/values to a hash is done via the => operator (known as fat comma) or just a plain comma. You use assignment operator =. You escaped a "compilation" error because you use perl-variables as your keys. So the line my $self = {$LogLine = '', $app = shift}; assigns the empty string to $LogLine but does not insert a key named $LogLine into the hash. Perhaps you wanted this?: my $self = {'LogLine' => '', 'app' => shift}; and access it using: $self->{'LogLine'}. The single quotes are superfluous but I am using it just to make a point and to show you that using them will allow you to have a key literally named '$LogLine' but I suspect this is not what you want. So, first use use strict; use warnings; in your package and then I guess you want to replace all $LogLine with 'LogLine' or just LogLine

        Well, I'm not sure what I wanted, but I was following this tutorial, badly, apparently, because they do have => instead of =. You're also correct about the name of the key.

        I made those two changes and added use warnings; use strict; and all is well. Thanks again!

        Updated code:
        package Logger; use warnings; use strict; use Mojo::Log; sub new { my $class = shift; my $self = {LogLine => '', appLog => Mojo::Log->new( path => './lo +gs/mojo-log', level => 'debug' )}; bless $self, $class; return $self; } sub add { my ( $self, $LogItem ) = @_; my $time = FormatDate(); $self->{LogLine} .= "[ $time ] :: $LogItem\n" if defined($LogItem) +; $self->{appLog}->debug($LogItem); } sub get { my( $self ) = @_; return $self->{LogLine}; } sub clear { my( $self ) = @_; $self->{LogLine} = ''; } sub FormatDate { my $Offset = shift; # number of seconds to offset the current time +. Example: FormatDate(3600) returns the time one hour from now. $Offset = 0 unless ($Offset); my ($Second, $Minute, $Hour, $Day, $Month, $Year, $WeekDay, $DayOf +Year, $IsDST) = localtime(time+$Offset); $Year = $Year + 1900; $Month = $Month + 1; $Day = sprintf ("%02s", $Day); $Month = sprintf ("%02s", $Month); $Hour = sprintf ("%02s", $Hour); $Minute = sprintf ("%02s", $Minute); $Second = sprintf ("%02s", $Second); return "$Year-$Month-$Day $Hour:$Minute:$Second"; } 1;
        I decided to create a Mojo::Log object within my Logger class -- because I still want every call to Logger::add() to write to the Mojo log file as well. Creating a new instance of Mojo::Log inside the class seemed to work better than creating it in the main app and passing it in. Thanks again bliako for your suggestions.