Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add -log option to allow users to disable Log::Any::App via env LOG

  • Loading branch information...
commit 363127a9ca290c38a81e1fdde546f0fd1da04028 1 parent fe7695e
Steven Haryanto authored June 24, 2012

Showing 1 changed file with 27 additions and 11 deletions. Show diff stats Hide diff stats

  1. 38  lib/Log/Any/App.pm
38  lib/Log/Any/App.pm
@@ -55,7 +55,7 @@ sub init {
55 55
     $caller ||= caller();
56 56
 
57 57
     my $spec = _parse_opts($args, $caller);
58  
-    _init_log4perl($spec) if $spec->{init};
  58
+    _init_log4perl($spec) if $spec->{log} && $spec->{init};
59 59
     $spec;
60 60
 }
61 61
 
@@ -316,15 +316,6 @@ sub _parse_opts {
316 316
     $args = _ifdef($args, []); # if we don't import(), we never get args
317 317
     _debug("parse_opts: args = [".join(", ", @$args)."]");
318 318
 
319  
-    my $spec = {
320  
-        name => _basename($0),
321  
-        init => 1,
322  
-        dump => ($ENV{LOGANYAPP_DEBUG} ? 1:0),
323  
-        daemon => 0,
324  
-        category_alias => _ifdefmj($ENV{LOG_CATEGORY_ALIAS}, {}),
325  
-        level_flag_paths => [File::HomeDir->my_home, "/etc"],
326  
-    };
327  
-
328 319
     my $i = 0;
329 320
     my %opts;
330 321
     while ($i < @$args) {
@@ -337,11 +328,23 @@ sub _parse_opts {
337 328
         $i++;
338 329
     }
339 330
 
  331
+    my $spec = {};
  332
+
  333
+    $spec->{log} = $ENV{LOG} // 1;
  334
+    if (defined $opts{log}) {
  335
+        $spec->{log} = $opts{log};
  336
+        delete $opts{log};
  337
+    }
  338
+    # exit as early as possible if we are not doing any logging
  339
+    goto END_PARSE_OPTS unless $spec->{log};
  340
+
  341
+    $spec->{name} = _basename($0);
340 342
     if (defined $opts{name}) {
341 343
         $spec->{name} = $opts{name};
342 344
         delete $opts{name};
343 345
     }
344 346
 
  347
+    $spec->{level_flag_paths} = [File::HomeDir->my_home, "/etc"];
345 348
     if (defined $opts{level_flag_paths}) {
346 349
         $spec->{level_flag_paths} = $opts{level_flag_paths};
347 350
         delete $opts{level_flag_paths};
@@ -357,6 +360,7 @@ sub _parse_opts {
357 360
     }
358 361
     delete $opts{level};
359 362
 
  363
+    $spec->{category_alias} = _ifdefmj($ENV{LOG_CATEGORY_ALIAS}, {});
360 364
     if (defined $opts{category_alias}) {
361 365
         die "category_alias must be a hashref"
362 366
             unless ref($opts{category_alias}) eq 'HASH';
@@ -375,17 +379,20 @@ sub _parse_opts {
375 379
         delete $opts{category_level};
376 380
     }
377 381
 
  382
+    $spec->{init} = 1;
378 383
     if (defined $opts{init}) {
379 384
         $spec->{init} = $opts{init};
380 385
         delete $opts{init};
381 386
     }
382 387
 
  388
+    $spec->{daemon} = 0;
383 389
     if (defined $opts{daemon}) {
384 390
         $spec->{daemon} = $opts{daemon};
385 391
         $is_daemon = $opts{daemon};
386 392
         delete $opts{daemon};
387 393
     }
388 394
 
  395
+    $spec->{dump} = $ENV{LOGANYAPP_DEBUG};
389 396
     if (defined $opts{dump}) {
390 397
         $spec->{dump} = 1;
391 398
         delete $opts{dump};
@@ -409,10 +416,11 @@ sub _parse_opts {
409 416
 
410 417
     if (keys %opts) {
411 418
         die "Unknown option(s) ".join(", ", keys %opts)." Known opts are: ".
412  
-            "name, level, category_level, category_alias, dump, init, ".
  419
+            "log, name, level, category_level, category_alias, dump, init, ".
413 420
                 "file, dir, screen, syslog";
414 421
     }
415 422
 
  423
+  END_PARSE_OPTS:
416 424
     #use Data::Dumper; print Dumper $spec;
417 425
     $spec;
418 426
 }
@@ -1283,6 +1291,14 @@ Arguments to init can be one or more of:
1283 1291
 
1284 1292
 =over 4
1285 1293
 
  1294
+=item -log => BOOL
  1295
+
  1296
+Whether to do log at all. Default is from LOG environment variable, or 1. This
  1297
+option is only to allow users to disable Log::Any::App (thus speeding up startup
  1298
+by avoiding loading Log4perl, etc) by passing LOG=1 environment when running
  1299
+programs. However, if you explicitly set this option to 1, Log::Any::App cannot
  1300
+be disabled this way.
  1301
+
1286 1302
 =item -init => BOOL
1287 1303
 
1288 1304
 Whether to call Log::Log4perl->init() after setting up the Log4perl

0 notes on commit 363127a

Please sign in to comment.
Something went wrong with that request. Please try again.