OSDN Git Service

add config parameter "Log.system_log"
authorhylom <hylom@users.sourceforge.jp>
Mon, 23 Oct 2017 11:23:29 +0000 (20:23 +0900)
committerhylom <hylom@users.sourceforge.jp>
Mon, 23 Oct 2017 11:23:29 +0000 (20:23 +0900)
src/newslash_web/lib/Newslash/Web.pm

index 93125d5..e6c3e3b 100644 (file)
@@ -21,6 +21,33 @@ sub startup {
     }
     # TODO: load/save configs with database
 
+    # set log file
+    if ($app->config->{Log} && $app->config->{Log}->{system_log}) {
+        # check log is writable
+        my $pathname = $app->config->{Log}->{system_log};
+        if (-e $pathname) {
+            if (-w $pathname) {
+                $app->log->debug("logs will be outputed to $pathname ...");
+                $app->log(Mojo::Log->new);
+                $app->log->path($pathname);
+            }
+            else {
+                $app->log->error("cannot write system log to file: $pathname");
+            }
+        }
+        else {
+            if (open(my $fh, ">", $pathname)) {
+                close($fh);
+                $app->log->debug("logs will be outputed to $pathname ...");
+                $app->log(Mojo::Log->new);
+                $app->log->path($pathname);
+            }
+            else {
+                $app->log->error("cannot create system log file: $pathname");
+            }
+        }
+    }
+
     # set log level
     if ($app->config->{Log} && $app->config->{Log}->{level}) {
         my $loglv = $app->config->{Log}->{level};