Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: Logging to web page with Mojolicious and strict

by bliako (Monsignor)
on Sep 14, 2019 at 09:00 UTC ( [id://11106167]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Logging to web page with Mojolicious and strict
in thread Logging to web page with Mojolicious and strict

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

Replies are listed 'Best First'.
Re^4: Logging to web page with Mojolicious and strict
by TieUpYourCamel (Scribe) on Sep 16, 2019 at 12:19 UTC
    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!

Re^4: Logging to web page with Mojolicious and strict
by TieUpYourCamel (Scribe) on Sep 16, 2019 at 15:36 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11106167]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found