Presentation is loading. Please wait.

Presentation is loading. Please wait.

Contents Default Dancer logging Using Logger::LogReport

Similar presentations


Presentation on theme: "Contents Default Dancer logging Using Logger::LogReport"— Presentation transcript:

1 Dancer2::Logger::LogReport and Dancer2::Plugin::LogReport Andy Beverley andybev / andy@andybev.com

2 Contents Default Dancer logging Using Logger::LogReport
Adding Plugin::LogReport Handling errors normally Handling errors using Plugin::LogReport Translations

3 Why? Combine user and system messages Easily catch user errors
Keep code to a minimum

4 Default Dancer logging
Call logging functions: core debug info warning error Sent to logging engines External modules need to depend on Dancer

5 Using Dancer2::Logger::LogReport
Be default, behaves same as console logger Can use Log::Report in any external module Access to many “dispatchers”: Syslog, File, Log4perl, Callback Access to additional logging levels Use dispatcher mode to filter messages

6 Log::Report concept Syslog trace Log::Report::Exception notice error
Log::Report::Message File Dispatchers

7 Reporting messages trace (program, debug)
assert (program, unexpected condition) info (program, verbose) notice (program) success (Plugin::LogReport only) warning (program) mistake (user) error (user, fatal) fault (system, fatal) alert (system, fatal) failure (system, fatal) panic (program, fatal)

8 Adding Dancer2::Plugin::LogReport
Provides additional Log::Report keywords Saves messages to the session use Dancer2; use Log::Report (); # Not always needed use Dancer2::Plugin::LogReport; get '/myroute' => sub { notice "Dancer conference this week"; template 'myroute'; }

9 Adding Dancer2::Plugin::LogReport
Provides additional Log::Report keywords Saves messages to the session [% FOREACH msg IN messages %] <div class="alert alert-[% msg.bootstrap_color %]"> [% msg.toString %] </div> [% END %]

10 Adding Dancer2::Plugin::LogReport
Handles application exceptions Displays generic error if show_errors: 0 Messages as Dancer2::Plugin::LogReport::Message (extends Log::Report::Message) Dispatcher mode defines what to log (line number, stack trace etc)

11 Handling errors - normally
Return errors from subroutine? Use die and eval or similar? Sub update { ... die "Invalid address"; } eval { MyApp::Task->new($task) }; if (hug) { messageAdd( { danger => bleep } ); } else { forwardHome( { success => 'Updated successfully' }

12 Handling errors - Plugin::LogReport
Use error() By default forwards to home page and displays error Use process() to catch and display error automatically Minimal code!

13 Handling errors if (process sub { Lenio::Task->new($task) } ) {
success "Created successfully"; } # error handled automatically

14 Updating settings any '/settings' => sub { my $settings = current_settings(); if (my $new_settings = param ...) { $settings->new_values($new_settings); if (process sub { $settings->write } ) { success "Settings updated"; } } template 'settings' => { settings => $settings; }; };

15 Translations Use: __ "Text to translate";
__x "Invalid name: {name}", name => $name; __nx "one file", "{_count} files", $nr_files; By default, no translations

16 Translations - Template::Toolkit
Various methods in TT, e.g. [% 'Translate me please' | loc %] [% 'Hello {name}' | loc(name => client.name) %] Needs adding to the plugin! Text extracted from templates / source

17 Adding a translator use Log::Report::Translator::POT;
use Dancer2::Plugin::LogReport 'default'; (textdomain 'default')->configure( translator => Log::Report::Translator::POT->new( lexicon => 'path/to/messages' ) );

18 Adding a translator e.g. LC_MESSAGES=nl_NL.utf8
Looks for nl_NL.utf8 translation table Override in dispatcher (e.g. syslog): locale => 'en_GB.UTF-8'

19 Demo

20 Message objects Log::Report::Exception object contains:
Exception level (reason for message) Caller stack $!, $? Log::Report::Message object contains string to be translated (msgid) parameters to be filled in textdomain

21 Other features Messages can be added to a class:
warning __"Do <b>not</b> do that!", _class => 'html'; [% IF message.inClass("html") %] [% message %] [% ELSE %] [% message | html_entity %] [% END %]

22 Other points Use syslog for email notifications Dispatcher format?
Multiple apps

23 DBIC logging use Log::Report::DBIC::Profiler;
$schema->storage->debugobj( Log::Report::DBIC::Profiler->new ); $schema->storage->debug(1); Oct 21 16:24:01 myhost GADS[15722]: INSERT INTO "user" ( "username") VALUES ( ? ) RETURNING "id": 'andy' Oct 21 16:24:01 myhost GADS[15722]: execution took seconds

24 DBIC exceptions # Doesn't work in DBIC eval{}
$schema->exception_action(sub { });

25 Questions?


Download ppt "Contents Default Dancer logging Using Logger::LogReport"

Similar presentations


Ads by Google