• R/O
  • HTTP
  • SSH
  • HTTPS

newslash: Commit

newslash


Commit MetaInfo

Revisão9562eaa47f48156613745208f5c8d35c127d3b27 (tree)
Hora2019-06-18 19:22:25
Autorhylom <hylom@user...>
Commiterhylom

Mensagem de Log

FeedFetcher: suppress verbose messages by MOJO_LOG_LEVEL environ var

Mudança Sumário

Diff

--- a/src/newslash_web/lib/Newslash/FeedFetcher.pm
+++ b/src/newslash_web/lib/Newslash/FeedFetcher.pm
@@ -13,6 +13,8 @@ use Data::Dumper;
1313 use DateTime::Format::MySQL;
1414 use Newslash::Util;
1515 use File::Spec;
16+use Mojo::Log;
17+
1618
1719 sub new {
1820 my $class = shift;
@@ -64,8 +66,11 @@ sub _load_config {
6466 my $home = $self->_detect_home;
6567 if ($home) {
6668 $config_file = File::Spec->catdir($home, "newslash-feedfetcher.yaml");
69+ if (! -e $config_file) {
70+ $config_file = File::Spec->catdir($home, "newslash-web.yaml");
71+ }
6772 }
68- }
73+ }
6974
7075 my $config = eval { LoadFile($config_file) };
7176 die qq{Can't parse config "$config_file": $@} if $@;
@@ -76,7 +81,14 @@ sub _load_config {
7681
7782 sub run {
7883 my $self = shift;
79- my $verbose = 2;
84+ my $log = Mojo::Log->new;
85+
86+ if ($ENV{MOJO_LOG_LEVEL}) {
87+ $log->level($ENV{MOJO_LOG_LEVEL});
88+ }
89+ else {
90+ $log->level("warn");
91+ }
8092
8193 my $config = $self->_load_config;
8294 $self->config($config);
@@ -90,16 +102,16 @@ sub run {
90102 }
91103 for my $feed (@$rs) {
92104 if ($feed->{status} ne "active") {
93- print "$feed->{title} is not active. skip ...";
105+ $log->info("$feed->{title} is not active. skip ...");
94106 next;
95107 }
96- print "fetch $feed->{url} ...\n" if $verbose > 0;
108+ $log->info("fetch $feed->{url} ...");
97109
98110 my $f = $feeds->fetch_url(\$feed->{url}, $config->{FeedFetcher} || {});
99111 my $now = DateTime::Format::MySQL->format_datetime(DateTime->now);
100112 if (!$f) {
101113 my $error = $feeds->last_error;
102- warn "fetch $feed->{url} failed - $error.\n";
114+ $log->error("fetch $feed->{url} failed - $error.");
103115 $feeds->update(feed_id => $feed->{feed_id},
104116 latest_fetch_time => $now,
105117 latest_fetch_result => $feeds->last_errorno);
@@ -126,14 +138,14 @@ sub run {
126138 );
127139 if (!defined $id) {
128140 my $error = $feeds->last_error;
129- warn "cannot create feed item: $error";
141+ $log->error("cannot create feed item: $error");
130142 }
131143 else {
132144 if ($id == 0) {
133- print "update item " . $item->link . " (feed_id: $feed->{feed_id})\n" if $verbose > 1;
145+ $log->debug("update item " . $item->link . " (feed_id: $feed->{feed_id})");
134146 }
135147 else {
136- print "create item " . $item->link . " (feed_id: $feed->{feed_id}, item_id: $id)\n" if $verbose > 1;
148+ $log->debug("create item " . $item->link . " (feed_id: $feed->{feed_id}, item_id: $id)");
137149 }
138150 }
139151 }
Show on old repository browser