jjmerelo
jjmerelo

Reputation: 23537

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?

Upvotes: 1

Views: 600

Answers (2)

szabgab
szabgab

Reputation: 6302

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.

Upvotes: 3

David Collins
David Collins

Reputation: 3032

1. Ensuring the configuration file is read

My only suggestions here are

  1. Ensure that you have the YAML module installed.
  2. Check the permissions on the configuration file - i.e. that the user running the script has read permissions.
  3. Check the location and name of the configuration file. It should be one of the the following: a) appdir/config.yml, b) appdir/environments/development.yml, or c) appdir/environments/production.yml (where appdir is where your Dancer application is located). See here for more info.

You also mentioned that you

tried to use the JSON configuration option.

I'm not sure what you mean by this. (The configuration file is written in YAML - not JSON.)

2. Logging configuration

You wrote above that

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)

I think this might be one of your problems. If you are setting the configuration programmatically (i.e. not in the configuration file), try placing

set logger => 'File';

after

set engines => { ...`

I.e., instead of

set logger => 'File';
set engines => {
    logger => {
        File => {
            log_dir => $log_dir,
            log_level => $log_level,
            file_name => $log_filename,
        }
    }
};

try the following.

set engines => {
    logger => {
        File => {
            log_dir => $log_dir,
            log_level => $log_level,
            file_name => $log_filename,
        }
    }
};
set logger => 'File';

The second approach works for me. The former doesn't.

1 https://metacpan.org/pod/Dancer2::Manual#CONFIGURATION .

Upvotes: 2

Related Questions