perlweb-servicesloggingdancer

Dancer2 not writing anything to a log file or maybe not reading config file


Here's the Dancer2 mini-app:

#!/usr/bin/env perl

use v5.14;

use Dancer2;
use File::Slurper qw(read_text);

set content_type => 'application/json';

my $path;
for my $p ( qw( hitos.json /data/hitos.json ./data/hitos.json ../data/hitos.json) ) {
  if ( -r $p ) {
    $path = $p;
  }
}

my $hitos = from_json read_text($path);

get '/status' => sub {
  return to_json { status => 'OK' };
};

get '/all' => sub {
  return to_json $hitos;
};

start;

with this configuration file:

logger : "File"
engines:
  logger:
    File:
      log_level: core
      log_dir: "/tmp"
      file_name: "p5hitos.log"

I have named it config.yml and config.yaml, and also tried to use the JSON configuration option. I have tried to set the port from it, and it does not "catch" the port setting, so could the problem be the configuration file failing silently? I have also tried to set the configuration on the same file:

set content_type => 'application/json';
set logger => "File";
set port => 31415;
set engines => { logger => { File => { log_level => "core",
                       log_dir => ".",
                       file_name => "p5hitos.log" }}};

In this case, the port and content-type are set correctly, but still no go, either with this log_dir or with /tmp. I have also tried code from this test and copied it verbatim (set logger after set engines and low-case file were the only differences). It does not change. config is still the same:

0  HASH(0x2a3d070)
   'appdir' => '/home/jmerelo/'
   'apphandler' => 'Standalone'
   'behind_proxy' => 0
   'charset' => ''
   'content_type' => 'application/json'
   'engines' => HASH(0x2947a58)
      'logger' => HASH(0x2947008)
         'File' => HASH(0xa438b0)
            'file_name' => 'p5hitos.log'
            'log_dir' => '/tmp'
   'environment' => 'development'
   'host' => '0.0.0.0'
   'logger' => Dancer2::Logger::File=HASH(0x2a7ef58)
      'app_name' => 'main'
      'config' => HASH(0x2a7f1b0)
           empty hash
      'environment' => 'development'
      'file_name' => 'p5hitos.log'
      'location' => '/home/jmerelo/'
      'log_dir' => '/tmp'
      'log_format' => '[%a:%P] %L @%T> %m in %f l. %l'
      'log_level' => 'debug'
   'no_server_tokens' => 0
   'port' => 31415
   'public_dir' => '/home/jmerelo/public'
   'route_handlers' => ARRAY(0x2779490)
      0  ARRAY(0x27791d8)
         0  'AutoPage'
         1  1
   'startup_info' => 1
   'static_handler' => undef
   'template' => 'Tiny'
   'traces' => 0
   'views' => '/home/jmerelo/views'

It starts up and returns the two routes correctly, but the log file is not created and obviously nothing is written on it. There's no error either on the console, and it's not logging to it either. It just shows the startup message: >> Dancer2 v0.204001 server 16427 listening on http://0.0.0.0:3000. The Log file object seems to have been created correctly, but it does not respond. Any idea?


Solution

  • app.psgi looks like this:

    use v5.14;
    use Dancer2;
    set content_type => 'application/json';
    get '/' => sub {
        debug "Hello World";
        return to_json config;
    };
    start;
    

    In the same directory config.yml looks like this:

    param: Foo
    logger : "File"
    engines:
      logger:
        File:
          log_level: core
          log_dir: "/tmp"
          file_name: "p5hitos.log"
    

    I launch the application as plackup app.psgi and then access it from another window as curl http://0:5000/ | jq

    The log entry is saved in the /tmp/p5hitos.log as expected and in the output on the command line I can see all the configuration values. Both the default ones and the ones I provided.