File: Dispatchouli.pm

package info (click to toggle)
liblog-dispatchouli-perl 3.009-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: perl: 854; makefile: 2
file content (1431 lines) | stat: -rw-r--r-- 40,388 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
use v5.20;
use warnings;
package Log::Dispatchouli 3.009;
# ABSTRACT: a simple wrapper around Log::Dispatch

# Not dangerous.  Accepted without change.
use experimental 'postderef', 'signatures';

use Carp ();
use File::Spec ();
use Log::Dispatch;
use Log::Fmt ();
use Params::Util qw(_ARRAY0 _HASH0 _CODELIKE);
use Scalar::Util qw(blessed weaken);
use String::Flogger;
use Try::Tiny 0.04;

require Log::Dispatchouli::Proxy;

our @CARP_NOT = qw(Log::Dispatchouli::Proxy);

#pod =head1 SYNOPSIS
#pod
#pod   my $logger = Log::Dispatchouli->new({
#pod     ident     => 'stuff-purger',
#pod     facility  => 'daemon',
#pod     to_stdout => $opt->{print},
#pod     debug     => $opt->{verbose}
#pod   });
#pod
#pod   $logger->log([ "There are %s items left to purge...", $stuff_left ]);
#pod
#pod   $logger->log_debug("this is extra often-ignored debugging log");
#pod
#pod   $logger->log_fatal("Now we will die!!");
#pod
#pod =head1 DESCRIPTION
#pod
#pod Log::Dispatchouli is a thin layer above L<Log::Dispatch> and meant to make it
#pod dead simple to add logging to a program without having to think much about
#pod categories, facilities, levels, or things like that.  It is meant to make
#pod logging just configurable enough that you can find the logs you want and just
#pod easy enough that you will actually log things.
#pod
#pod Log::Dispatchouli can log to syslog (if you specify a facility), standard error
#pod or standard output, to a file, or to an array in memory.  That last one is
#pod mostly useful for testing.
#pod
#pod In addition to providing as simple a way to get a handle for logging
#pod operations, Log::Dispatchouli uses L<String::Flogger> to process the things to
#pod be logged, meaning you can easily log data structures.  Basically: strings are
#pod logged as is, arrayrefs are taken as (sprintf format, args), and subroutines
#pod are called only if needed.  For more information read the L<String::Flogger>
#pod docs.
#pod
#pod =head1 LOGGER PREFIX
#pod
#pod Log messages may be prepended with information to set context.  This can be set
#pod at a logger level or per log item.  The simplest example is:
#pod
#pod   my $logger = Log::Dispatchouli->new( ... );
#pod
#pod   $logger->set_prefix("Batch 123: ");
#pod
#pod   $logger->log("begun processing");
#pod
#pod   # ...
#pod
#pod   $logger->log("finished processing");
#pod
#pod The above will log something like:
#pod
#pod   Batch 123: begun processing
#pod   Batch 123: finished processing
#pod
#pod To pass a prefix per-message:
#pod
#pod   $logger->log({ prefix => 'Sub-Item 234: ' }, 'error!')
#pod
#pod   # Logs: Batch 123: Sub-Item 234: error!
#pod
#pod If the prefix is a string, it is prepended to each line of the message.  If it
#pod is a coderef, it is called and passed the message to be logged.  The return
#pod value is logged instead.
#pod
#pod L<Proxy loggers|/METHODS FOR PROXY LOGGERS> also have their own prefix
#pod settings, which accumulate.  So:
#pod
#pod   my $proxy = $logger->proxy({ proxy_prefix => 'Subsystem 12: ' });
#pod
#pod   $proxy->set_prefix('Page 9: ');
#pod
#pod   $proxy->log({ prefix => 'Paragraph 6: ' }, 'Done.');
#pod
#pod ...will log...
#pod
#pod   Batch 123: Subsystem 12: Page 9: Paragraph 6: Done.
#pod
#pod =method new
#pod
#pod   my $logger = Log::Dispatchouli->new(\%arg);
#pod
#pod This returns a new logger, a Log::Dispatchouli object.
#pod
#pod Valid arguments are:
#pod
#pod   ident       - the name of the thing logging (mandatory)
#pod   to_self     - log to the logger object for testing; default: false
#pod   to_stdout   - log to STDOUT; default: false
#pod   to_stderr   - log to STDERR; default: false
#pod   facility    - to which syslog facility to send logs; default: none
#pod
#pod   to_file     - DEPRECATED: this option will be removed in 2025
#pod                 log to PROGRAM_NAME.YYYYMMDD in the log path; default: false
#pod   log_file    - a leaf name for the file to log to with to_file
#pod   log_path    - path in which to log to file; defaults to DISPATCHOULI_PATH
#pod                 environment variable or, failing that, to your system's tmpdir
#pod
#pod   file_format - this optional coderef is passed the message to be logged
#pod                 and returns the text to write out
#pod
#pod   log_pid     - if 1, prefix all log entries with the pid; default: true
#pod                 can also be a comma-delimited list of log targets where pid is
#pod                 logged, like "stderr,syslog"; mostly useful for logging pid in
#pod                 syslog, but not on standard I/O
#pod   fail_fatal  - a boolean; if true, failure to log is fatal; default: true
#pod   muted       - a boolean; if true, only fatals are logged; default: false
#pod   debug       - a boolean; if true, log_debug method is not a no-op
#pod                 defaults to the truth of the DISPATCHOULI_DEBUG env var
#pod   quiet_fatal - 'stderr' or 'stdout' or an arrayref of zero, one, or both
#pod                 fatal log messages will not be logged to these
#pod                 (default: stderr)
#pod   config_id   - a name for this logger's config; rarely needed!
#pod   syslog_socket - a value for Sys::Syslog's "socket" arg; default: "native"
#pod
#pod The log path is either F</tmp> or the value of the F<DISPATCHOULI_PATH> env var.
#pod
#pod If the F<DISPATCHOULI_NOSYSLOG> env var is true, we don't log to syslog.
#pod
#pod =cut

sub new {
  my ($class, $arg) = @_;

  my $ident = $arg->{ident}
    or Carp::croak "no ident specified when using $class";

  my $config_id = defined $arg->{config_id} ? $arg->{config_id} : $ident;

  my %quiet_fatal;
  for ('quiet_fatal') {
    %quiet_fatal = map {; $_ => 1 } grep { defined }
      exists $arg->{$_}
        ? _ARRAY0($arg->{$_}) ? @{ $arg->{$_} } : $arg->{$_}
        : ('stderr');
  };

  my $log = Log::Dispatch->new;
  my $self = bless {
    dispatcher => $log,
    log_pid    => (exists $arg->{log_pid} ? $arg->{log_pid} : 1),
  } => $class;

  if ($arg->{to_file}) {
    Carp::carp("to_file argument for Log::Dispatchouli is deprecated and will be removed in late 2025");

    require Log::Dispatch::File;
    my $log_file = File::Spec->catfile(
      ($arg->{log_path} || $self->env_value('PATH') || File::Spec->tmpdir),
      $arg->{log_file} || do {
        my @time = localtime;
        sprintf('%s.%04u%02u%02u',
          $ident,
          $time[5] + 1900,
          $time[4] + 1,
          $time[3])
      }
    );

    $log->add(
      Log::Dispatch::File->new(
        name      => 'logfile',
        min_level => 'debug',
        filename  => $log_file,
        mode      => 'append',
        callbacks => do {
          my $log_pid = $self->log_pid_for('file');

          if (my $format = $arg->{file_format}) {
            sub {
              my $message = {@_}->{message};
              $message = "[$$] $message" if $log_pid;
              $format->($message)
            };
          } else {
            # The time format returned here is subject to change. -- rjbs,
            # 2008-11-21
            sub {
              my $message = {@_}->{message};
              $message = "[$$] $message" if $log_pid;
              (localtime) . " $message\n";
            };
          }
        },
      )
    );
  }

  if ($arg->{facility} and not $self->env_value('NOSYSLOG')) {
    $self->setup_syslog_output(
      facility  => $arg->{facility},
      socket    => $arg->{syslog_socket},
      ident     => $ident,
    );
  }

  if ($arg->{to_self}) {
    $self->{events} = [];
    require Log::Dispatch::Array;
    $log->add(
      Log::Dispatch::Array->new(
        name      => 'self',
        min_level => 'debug',
        array     => $self->{events},
        ($self->log_pid_for('self')
          ? (callbacks => sub { "[$$] ". {@_}->{message} })
          : ())
      ),
    );
  }

  $self->{prefix}     = $arg->{prefix};
  $self->{ident}      = $ident;
  $self->{config_id}  = $config_id;

  DEST: for my $dest (qw(err out)) {
    next DEST unless $arg->{"to_std$dest"};
    my $method = "enable_std$dest";

    $self->$method;
  }

  $self->{debug}  = exists $arg->{debug}
                  ? ($arg->{debug} ? 1 : 0)
                  : ($self->env_value('DEBUG') ? 1 : 0);
  $self->{muted}  = $arg->{muted};

  $self->{quiet_fatal} = \%quiet_fatal;
  $self->{fail_fatal}  = exists $arg->{fail_fatal} ? $arg->{fail_fatal} : 1;

  return $self;
}

sub log_pid_for ($self, $output) {
  my $log_pid = $self->{log_pid};
  return undef unless $log_pid;

  return 1 if $log_pid eq 1;

  $self->{log_pid_for} = { map {; $_ => 1 } split /,/, $log_pid };

  return $self->{log_pid_for}{$output} ? 1 : undef;
}

#pod =method enable_stdout
#pod
#pod =method enable_stderr
#pod
#pod These methods turn on logging to STDOUT or STDERR, respectively.  If that
#pod logging has already been enabled, these methods do nothing.
#pod
#pod There is not, yet, a I<disable> version of these methods.
#pod
#pod =cut

for my $dest (qw(out err)) {
  my $name = "std$dest";
  my $code = sub ($self) {
    return if $self->dispatcher->output($name);

    my $callback = $self->log_pid_for($name)
                 ? sub { "[$$] " . ({@_}->{message}) . "\n" }
                 : sub {           ({@_}->{message}) . "\n" };

    $self->dispatcher->add(
      $self->stdio_dispatcher_class->new(
        name      => "std$dest",
        min_level => 'debug',
        stderr    => ($dest eq 'err' ? 1 : 0),
        callbacks => $callback,
        ($self->{quiet_fatal}{"std$dest"} ? (max_level => 'info') : ()),
      ),
    );
  };

  no strict 'refs';
  *{"enable_std$dest"} = $code;
}

sub setup_syslog_output {
  my ($self, %arg) = @_;

  require Log::Dispatch::Syslog;
  $self->{dispatcher}->add(
    Log::Dispatch::Syslog->new(
      name      => 'syslog',
      ident     => $arg{ident},
      facility  => $arg{facility},
      logopt    => ($self->log_pid_for('syslog') ? 'pid' : ''),
      min_level => 'debug',
      socket    => $arg{socket} || 'native',
      callbacks => sub {
        ( my $m = {@_}->{message} ) =~ s/\n/<LF>/g;
        $m
      },
    ),
  );
}

#pod =method log
#pod
#pod   $logger->log(@messages);
#pod
#pod   $logger->log(\%arg, @messages);
#pod
#pod This method uses L<String::Flogger> on the input, then I<unconditionally> logs
#pod the result.  Each message is flogged individually, then joined with spaces.
#pod
#pod If the first argument is a hashref, it will be used as extra arguments to
#pod logging.  It may include a C<prefix> entry to preprocess the message by
#pod prepending a string (if the prefix is a string) or calling a subroutine to
#pod generate a new message (if the prefix is a coderef).
#pod
#pod =cut

sub _join { shift; join q{ }, @{ $_[0] } }

sub log {
  my ($self, @rest) = @_;
  my $arg = _HASH0($rest[0]) ? shift(@rest) : {};

  my $message;

  if ($arg->{fatal} or ! $self->get_muted) {
    try {
      my $flogger = $self->string_flogger;
      my @flogged = map {; $flogger->flog($_) } @rest;
      $message    = @flogged > 1 ? $self->_join(\@flogged) : $flogged[0];

      my @prefix  = _ARRAY0($arg->{prefix})
                  ? @{ $arg->{prefix} }
                  : $arg->{prefix};

      for (reverse grep { defined } $self->get_prefix, @prefix) {
        if (_CODELIKE( $_ )) {
          $message = $_->($message);
        } else {
          $message =~ s/^/$_/gm;
        }
      }

      $self->dispatcher->log(
        level   => $arg->{level} || 'info',
        message => $message,
      );
    } catch {
      $message = '(no message could be logged)' unless defined $message;
      die $_ if $self->{fail_fatal};
    };
  }

  Carp::croak $message if $arg->{fatal};

  return;
}

#pod =method log_fatal
#pod
#pod This behaves like the C<log> method, but will throw the logged string as an
#pod exception after logging.
#pod
#pod This method can also be called as C<fatal>, to match other popular logging
#pod interfaces.  B<If you want to override this method, you must override
#pod C<log_fatal> and not C<fatal>>.
#pod
#pod =cut

sub log_fatal {
  my ($self, @rest) = @_;

  my $arg = _HASH0($rest[0]) ? shift(@rest) : {}; # for future expansion

  local $arg->{level} = defined $arg->{level} ? $arg->{level} : 'error';
  local $arg->{fatal} = defined $arg->{fatal} ? $arg->{fatal} : 1;

  $self->log($arg, @rest);
}

#pod =method log_debug
#pod
#pod This behaves like the C<log> method, but will only log (at the debug level) if
#pod the logger object has its debug property set to true.
#pod
#pod This method can also be called as C<debug>, to match other popular logging
#pod interfaces.  B<If you want to override this method, you must override
#pod C<log_debug> and not C<debug>>.
#pod
#pod =cut

sub log_debug {
  my ($self, @rest) = @_;

  return unless $self->is_debug;

  my $arg = _HASH0($rest[0]) ? shift(@rest) : {}; # for future expansion

  local $arg->{level} = defined $arg->{level} ? $arg->{level} : 'debug';

  $self->log($arg, @rest);
}

#pod =method log_event
#pod
#pod This method is like C<log>, but is used for structured logging instead of free
#pod form text.  It's invoked like this:
#pod
#pod   $logger->log($event_type => $data_ref);
#pod
#pod C<$event_type> should be a simple string, probably a valid identifier, that
#pod identifies the kind of event being logged.  It is suggested, but not required,
#pod that all events of the same type have the same kind of structured data in them.
#pod
#pod C<$data_ref> is a set of key/value pairs of data to log in this event.  It can
#pod be an arrayref (in which case the ordering of pairs is preserved) or a hashref
#pod (in which case they are sorted by key).
#pod
#pod The logged string will be in logfmt format, meaning a series of key=value
#pod pairs separated by spaces and following these rules:
#pod
#pod =for :list
#pod * an "identifier" is a string of printable ASCII characters between C<!> and
#pod   C<~>, excluding C<\> and C<=>
#pod * keys must be valid identifiers
#pod * if a key is empty, C<~> is used instead
#pod * if a key contains characters not permitted in an identifier, they are
#pod   replaced by C<?>
#pod * values must I<either> be valid identifiers, or be quoted
#pod * quoted value start and end with C<">
#pod * in a quoted value, C<"> becomes C<\">, C<\> becomes C<\\>, newline and
#pod   carriage return become C<\n> and C<\r> respectively, and other control
#pod   characters are replaced with C<\u{....}> where the contents of the braces are
#pod   the hex value of the control character
#pod
#pod When values are undef, they are represented as C<~>.
#pod
#pod When values are array references, the index/values are mapped over, so that:
#pod
#pod   key => [ 'a', 'b' ]
#pod
#pod becomes
#pod
#pod   key.0=a key.1=b
#pod
#pod When values are hash references, the key/values are mapped, with keys sorted,
#pod so that:
#pod
#pod   key => { b => 2, a => 1 }
#pod
#pod becomes
#pod
#pod   key.a=1 key.b=2
#pod
#pod This expansion is performed recursively.  If a value itself recurses,
#pod appearances of a reference after the first time will be replaced with a string
#pod like C<&foo.bar>, pointing to the first occurrence.  I<This is not meant to be
#pod a robust serialization mechanism.>  It's just here to help you be a little
#pod lazy.  Don't push the limits.
#pod
#pod If the value in C<$data_ref> is a code reference, it will be called and its
#pod result logged.  If its result is also a code reference, you get whatever
#pod garbage that code reference stringifies to.
#pod
#pod If the value in C<$data_ref> is a reference reference, then the referenced
#pod scalar will be passed to String::Flogger, and the resulting string will be used
#pod as the value to log.  That string will be quoted as described above, if needed.
#pod
#pod =cut

sub log_event {
  my ($self, $type, $data) = @_;

  return $self->_log_event($type, undef, $data);
}

sub _compute_proxy_ctx_kvstr_aref {
  return [];
}

sub _log_event {
  my ($self, $type, $ctx, $data) = @_;

  return if $self->get_muted;

  my $kv_aref = Log::Fmt->_pairs_to_kvstr_aref([
    event => $type,
    (_ARRAY0($data) ? @$data : $data->%{ sort keys %$data })
  ]);

  splice @$kv_aref, 1, 0, @$ctx if $ctx;

  $self->dispatcher->log(
    level   => 'info',
    message => join q{ }, @$kv_aref,
  );

  return;
}

#pod =method log_debug_event
#pod
#pod This method is just like C<log_event>, but will log nothing unless the logger
#pod has its C<debug> property set to true.
#pod
#pod =cut

sub log_debug_event {
  my ($self, $type, $data) = @_;

  return unless $self->get_debug;

  $self->log_event($type, $data);
}

#pod =method set_debug
#pod
#pod   $logger->set_debug($bool);
#pod
#pod This sets the logger's debug property, which affects the behavior of
#pod C<log_debug>.
#pod
#pod =cut

sub set_debug {
  return($_[0]->{debug} = $_[1] ? 1 : 0);
}

#pod =method get_debug
#pod
#pod This gets the logger's debug property, which affects the behavior of
#pod C<log_debug>.
#pod
#pod =cut

sub get_debug { return $_[0]->{debug} }

#pod =method clear_debug
#pod
#pod This method does nothing, and is only useful for L<Log::Dispatchouli::Proxy>
#pod objects.  See L<Methods for Proxy Loggers|/METHODS FOR PROXY LOGGERS>, below.
#pod
#pod =cut

sub clear_debug { }

sub mute   { $_[0]{muted} = 1 }
sub unmute { $_[0]{muted} = 0 }

#pod =method set_muted
#pod
#pod   $logger->set_muted($bool);
#pod
#pod This sets the logger's muted property, which affects the behavior of
#pod C<log>.
#pod
#pod =cut

sub set_muted {
  return($_[0]->{muted} = $_[1] ? 1 : 0);
}

#pod =method get_muted
#pod
#pod This gets the logger's muted property, which affects the behavior of
#pod C<log>.
#pod
#pod =cut

sub get_muted { return $_[0]->{muted} }

#pod =method clear_muted
#pod
#pod This method does nothing, and is only useful for L<Log::Dispatchouli::Proxy>
#pod objects.  See L<Methods for Proxy Loggers|/METHODS FOR PROXY LOGGERS>, below.
#pod
#pod =cut

sub clear_muted { }

#pod =method get_prefix
#pod
#pod   my $prefix = $logger->get_prefix;
#pod
#pod This method returns the currently-set prefix for the logger, which may be a
#pod string or code reference or undef.  See L<Logger Prefix|/LOGGER PREFIX>.
#pod
#pod =method set_prefix
#pod
#pod   $logger->set_prefix( $new_prefix );
#pod
#pod This method changes the prefix.  See L<Logger Prefix|/LOGGER PREFIX>.
#pod
#pod =method clear_prefix
#pod
#pod This method clears any set logger prefix.  (It can also be called as
#pod C<unset_prefix>, but this is deprecated.  See L<Logger Prefix|/LOGGER PREFIX>.
#pod
#pod =cut

sub get_prefix   { return $_[0]->{prefix}  }
sub set_prefix   { $_[0]->{prefix} = $_[1] }
sub clear_prefix { $_[0]->unset_prefix     }
sub unset_prefix { undef $_[0]->{prefix}   }

#pod =method ident
#pod
#pod This method returns the logger's ident.
#pod
#pod =cut

sub ident { $_[0]{ident} }

#pod =method config_id
#pod
#pod This method returns the logger's configuration id, which defaults to its ident.
#pod This can be used to make two loggers equivalent in Log::Dispatchouli::Global so
#pod that trying to reinitialize with a new logger with the same C<config_id> as the
#pod current logger will not throw an exception, and will simply do no thing.
#pod
#pod =cut

sub config_id { $_[0]{config_id} }

#pod =head1 METHODS FOR SUBCLASSING
#pod
#pod =head2 string_flogger
#pod
#pod This method returns the thing on which F<flog> will be called to format log
#pod messages.  By default, it just returns C<String::Flogger>
#pod
#pod =cut

sub string_flogger { 'String::Flogger' }

#pod =head2 env_prefix
#pod
#pod This method should return a string used as a prefix to find environment
#pod variables that affect the logger's behavior.  For example, if this method
#pod returns C<XYZZY> then when checking the environment for a default value for the
#pod C<debug> parameter, Log::Dispatchouli will first check C<XYZZY_DEBUG>, then
#pod C<DISPATCHOULI_DEBUG>.
#pod
#pod By default, this method returns C<()>, which means no extra environment
#pod variable is checked.
#pod
#pod =cut

sub env_prefix { return; }

#pod =head2 env_value
#pod
#pod   my $value = $logger->env_value('DEBUG');
#pod
#pod This method returns the value for the environment variable suffix given.  For
#pod example, the example given, calling with C<DEBUG> will check
#pod C<DISPATCHOULI_DEBUG>.
#pod
#pod =cut

sub env_value {
  my ($self, $suffix) = @_;

  my @path = grep { defined } ($self->env_prefix, 'DISPATCHOULI');

  for my $prefix (@path) {
    my $name = join q{_}, $prefix, $suffix;
    return $ENV{ $name } if defined $ENV{ $name };
  }

  return;
}

#pod =head1 METHODS FOR TESTING
#pod
#pod =head2 new_tester
#pod
#pod   my $logger = Log::Dispatchouli->new_tester( \%arg );
#pod
#pod This returns a new logger that logs only C<to_self>.  It's useful in testing.
#pod If no C<ident> arg is provided, one will be generated.  C<log_pid> is off by
#pod default, but can be overridden.
#pod
#pod C<\%arg> is optional.
#pod
#pod =cut

sub new_tester {
  my ($class, $arg) = @_;
  $arg ||= {};

  return $class->new({
    ident     => "$$:$0",
    log_pid   => 0,
    %$arg,
    to_stderr => 0,
    to_stdout => 0,
    to_file   => 0,
    to_self   => 1,
    facility  => undef,
  });
}

#pod =head2 events
#pod
#pod This method returns the arrayref of events logged to an array in memory (in the
#pod logger).  If the logger is not logging C<to_self> this raises an exception.
#pod
#pod =cut

sub events {
  Carp::confess "->events called on a logger not logging to self"
    unless $_[0]->{events};

  return $_[0]->{events};
}

#pod =head2 clear_events
#pod
#pod This method empties the current sequence of events logged into an array in
#pod memory.  If the logger is not logging C<to_self> this raises an exception.
#pod
#pod =cut

sub clear_events {
  Carp::confess "->events called on a logger not logging to self"
    unless $_[0]->{events};

  @{ $_[0]->{events} } = ();
  return;
}

#pod =head1 METHODS FOR PROXY LOGGERS
#pod
#pod =head2 proxy
#pod
#pod   my $proxy_logger = $logger->proxy( \%arg );
#pod
#pod This method returns a new proxy logger -- an instance of
#pod L<Log::Dispatchouli::Proxy> -- which will log through the given logger, but
#pod which may have some settings localized.
#pod
#pod C<%arg> is optional.  It may contain the following entries:
#pod
#pod =for :list
#pod = proxy_prefix
#pod This is a prefix that will be applied to anything the proxy logger logs, and
#pod cannot be changed.
#pod = proxy_ctx
#pod This is data to be inserted in front of event data logged through the proxy.
#pod It will appear I<after> the C<event> key but before the logged event data.  It
#pod can be in the same format as the C<$data_ref> argument to C<log_event>.
#pod = debug
#pod This can be set to true or false to change the proxy's "am I in debug mode?"
#pod setting.  It can be changed or cleared later on the proxy.
#pod
#pod =cut

sub proxy_class {
  return 'Log::Dispatchouli::Proxy';
}

sub proxy {
  my ($self, $arg) = @_;
  $arg ||= {};

  my $proxy = $self->proxy_class->_new({
    parent => $self,
    logger => $self,
    proxy_prefix => $arg->{proxy_prefix},
    (exists $arg->{debug} ? (debug => ($arg->{debug} ? 1 : 0)) : ()),
  });

  if (my $ctx = $arg->{proxy_ctx}) {
    $proxy->{proxy_ctx} = _ARRAY0($ctx)
                        ? [ @$ctx ]
                        : [ $ctx->%{ sort keys %$ctx } ];
  }

  return $proxy;
}

#pod =head2 parent
#pod
#pod =head2 logger
#pod
#pod These methods return the logger itself.  (They're more useful when called on
#pod proxy loggers.)
#pod
#pod =cut

sub parent { $_[0] }
sub logger { $_[0] }

#pod =method dispatcher
#pod
#pod This returns the underlying Log::Dispatch object.  This is not the method
#pod you're looking for.  Move along.
#pod
#pod =cut

sub dispatcher   { $_[0]->{dispatcher} }

#pod =method stdio_dispatcher_class
#pod
#pod This method is an experimental feature to allow you to pick an alternate
#pod dispatch class for stderr and stdio.  By default, Log::Dispatch::Screen is
#pod used.  B<This feature may go away at any time.>
#pod
#pod =cut

sub stdio_dispatcher_class {
  require Log::Dispatch::Screen;
  return 'Log::Dispatch::Screen';
}

#pod =head1 METHODS FOR API COMPATIBILITY
#pod
#pod To provide compatibility with some other loggers, most specifically
#pod L<Log::Contextual>, the following methods are provided.  You should not use
#pod these methods without a good reason, and you should never subclass them.
#pod Instead, subclass the methods they call.
#pod
#pod =begin :list
#pod
#pod = is_debug
#pod
#pod This method calls C<get_debug>.
#pod
#pod = is_info
#pod
#pod = is_fatal
#pod
#pod These methods return true.
#pod
#pod = info
#pod
#pod = fatal
#pod
#pod = debug
#pod
#pod These methods redispatch to C<log>, C<log_fatal>, and C<log_debug>
#pod respectively.
#pod
#pod =end :list
#pod
#pod =cut

sub is_debug { $_[0]->get_debug }
sub is_info  { 1 }
sub is_fatal { 1 }

sub info  { shift()->log(@_); }
sub fatal { shift()->log_fatal(@_); }
sub debug { shift()->log_debug(@_); }

use overload
  '&{}'    => sub { my ($self) = @_; sub { $self->log(@_) } },
  fallback => 1,
;

#pod =head1 SEE ALSO
#pod
#pod =for :list
#pod * L<Log::Dispatch>
#pod * L<String::Flogger>
#pod
#pod =cut

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Log::Dispatchouli - a simple wrapper around Log::Dispatch

=head1 VERSION

version 3.009

=head1 SYNOPSIS

  my $logger = Log::Dispatchouli->new({
    ident     => 'stuff-purger',
    facility  => 'daemon',
    to_stdout => $opt->{print},
    debug     => $opt->{verbose}
  });

  $logger->log([ "There are %s items left to purge...", $stuff_left ]);

  $logger->log_debug("this is extra often-ignored debugging log");

  $logger->log_fatal("Now we will die!!");

=head1 DESCRIPTION

Log::Dispatchouli is a thin layer above L<Log::Dispatch> and meant to make it
dead simple to add logging to a program without having to think much about
categories, facilities, levels, or things like that.  It is meant to make
logging just configurable enough that you can find the logs you want and just
easy enough that you will actually log things.

Log::Dispatchouli can log to syslog (if you specify a facility), standard error
or standard output, to a file, or to an array in memory.  That last one is
mostly useful for testing.

In addition to providing as simple a way to get a handle for logging
operations, Log::Dispatchouli uses L<String::Flogger> to process the things to
be logged, meaning you can easily log data structures.  Basically: strings are
logged as is, arrayrefs are taken as (sprintf format, args), and subroutines
are called only if needed.  For more information read the L<String::Flogger>
docs.

=head1 PERL VERSION

This library should run on perls released even a long time ago.  It should
work on any version of perl released in the last five years.

Although it may work on older versions of perl, no guarantee is made that the
minimum required version will not be increased.  The version may be increased
for any reason, and there is no promise that patches will be accepted to
lower the minimum required perl.

=head1 METHODS

=head2 new

  my $logger = Log::Dispatchouli->new(\%arg);

This returns a new logger, a Log::Dispatchouli object.

Valid arguments are:

  ident       - the name of the thing logging (mandatory)
  to_self     - log to the logger object for testing; default: false
  to_stdout   - log to STDOUT; default: false
  to_stderr   - log to STDERR; default: false
  facility    - to which syslog facility to send logs; default: none

  to_file     - DEPRECATED: this option will be removed in 2025
                log to PROGRAM_NAME.YYYYMMDD in the log path; default: false
  log_file    - a leaf name for the file to log to with to_file
  log_path    - path in which to log to file; defaults to DISPATCHOULI_PATH
                environment variable or, failing that, to your system's tmpdir

  file_format - this optional coderef is passed the message to be logged
                and returns the text to write out

  log_pid     - if 1, prefix all log entries with the pid; default: true
                can also be a comma-delimited list of log targets where pid is
                logged, like "stderr,syslog"; mostly useful for logging pid in
                syslog, but not on standard I/O
  fail_fatal  - a boolean; if true, failure to log is fatal; default: true
  muted       - a boolean; if true, only fatals are logged; default: false
  debug       - a boolean; if true, log_debug method is not a no-op
                defaults to the truth of the DISPATCHOULI_DEBUG env var
  quiet_fatal - 'stderr' or 'stdout' or an arrayref of zero, one, or both
                fatal log messages will not be logged to these
                (default: stderr)
  config_id   - a name for this logger's config; rarely needed!
  syslog_socket - a value for Sys::Syslog's "socket" arg; default: "native"

The log path is either F</tmp> or the value of the F<DISPATCHOULI_PATH> env var.

If the F<DISPATCHOULI_NOSYSLOG> env var is true, we don't log to syslog.

=head2 enable_stdout

=head2 enable_stderr

These methods turn on logging to STDOUT or STDERR, respectively.  If that
logging has already been enabled, these methods do nothing.

There is not, yet, a I<disable> version of these methods.

=head2 log

  $logger->log(@messages);

  $logger->log(\%arg, @messages);

This method uses L<String::Flogger> on the input, then I<unconditionally> logs
the result.  Each message is flogged individually, then joined with spaces.

If the first argument is a hashref, it will be used as extra arguments to
logging.  It may include a C<prefix> entry to preprocess the message by
prepending a string (if the prefix is a string) or calling a subroutine to
generate a new message (if the prefix is a coderef).

=head2 log_fatal

This behaves like the C<log> method, but will throw the logged string as an
exception after logging.

This method can also be called as C<fatal>, to match other popular logging
interfaces.  B<If you want to override this method, you must override
C<log_fatal> and not C<fatal>>.

=head2 log_debug

This behaves like the C<log> method, but will only log (at the debug level) if
the logger object has its debug property set to true.

This method can also be called as C<debug>, to match other popular logging
interfaces.  B<If you want to override this method, you must override
C<log_debug> and not C<debug>>.

=head2 log_event

This method is like C<log>, but is used for structured logging instead of free
form text.  It's invoked like this:

  $logger->log($event_type => $data_ref);

C<$event_type> should be a simple string, probably a valid identifier, that
identifies the kind of event being logged.  It is suggested, but not required,
that all events of the same type have the same kind of structured data in them.

C<$data_ref> is a set of key/value pairs of data to log in this event.  It can
be an arrayref (in which case the ordering of pairs is preserved) or a hashref
(in which case they are sorted by key).

The logged string will be in logfmt format, meaning a series of key=value
pairs separated by spaces and following these rules:

=over 4

=item *

an "identifier" is a string of printable ASCII characters between C<!> and C<~>, excluding C<\> and C<=>

=item *

keys must be valid identifiers

=item *

if a key is empty, C<~> is used instead

=item *

if a key contains characters not permitted in an identifier, they are replaced by C<?>

=item *

values must I<either> be valid identifiers, or be quoted

=item *

quoted value start and end with C<">

=item *

in a quoted value, C<"> becomes C<\">, C<\> becomes C<\\>, newline and carriage return become C<\n> and C<\r> respectively, and other control characters are replaced with C<\u{....}> where the contents of the braces are the hex value of the control character

=back

When values are undef, they are represented as C<~>.

When values are array references, the index/values are mapped over, so that:

  key => [ 'a', 'b' ]

becomes

  key.0=a key.1=b

When values are hash references, the key/values are mapped, with keys sorted,
so that:

  key => { b => 2, a => 1 }

becomes

  key.a=1 key.b=2

This expansion is performed recursively.  If a value itself recurses,
appearances of a reference after the first time will be replaced with a string
like C<&foo.bar>, pointing to the first occurrence.  I<This is not meant to be
a robust serialization mechanism.>  It's just here to help you be a little
lazy.  Don't push the limits.

If the value in C<$data_ref> is a code reference, it will be called and its
result logged.  If its result is also a code reference, you get whatever
garbage that code reference stringifies to.

If the value in C<$data_ref> is a reference reference, then the referenced
scalar will be passed to String::Flogger, and the resulting string will be used
as the value to log.  That string will be quoted as described above, if needed.

=head2 log_debug_event

This method is just like C<log_event>, but will log nothing unless the logger
has its C<debug> property set to true.

=head2 set_debug

  $logger->set_debug($bool);

This sets the logger's debug property, which affects the behavior of
C<log_debug>.

=head2 get_debug

This gets the logger's debug property, which affects the behavior of
C<log_debug>.

=head2 clear_debug

This method does nothing, and is only useful for L<Log::Dispatchouli::Proxy>
objects.  See L<Methods for Proxy Loggers|/METHODS FOR PROXY LOGGERS>, below.

=head2 set_muted

  $logger->set_muted($bool);

This sets the logger's muted property, which affects the behavior of
C<log>.

=head2 get_muted

This gets the logger's muted property, which affects the behavior of
C<log>.

=head2 clear_muted

This method does nothing, and is only useful for L<Log::Dispatchouli::Proxy>
objects.  See L<Methods for Proxy Loggers|/METHODS FOR PROXY LOGGERS>, below.

=head2 get_prefix

  my $prefix = $logger->get_prefix;

This method returns the currently-set prefix for the logger, which may be a
string or code reference or undef.  See L<Logger Prefix|/LOGGER PREFIX>.

=head2 set_prefix

  $logger->set_prefix( $new_prefix );

This method changes the prefix.  See L<Logger Prefix|/LOGGER PREFIX>.

=head2 clear_prefix

This method clears any set logger prefix.  (It can also be called as
C<unset_prefix>, but this is deprecated.  See L<Logger Prefix|/LOGGER PREFIX>.

=head2 ident

This method returns the logger's ident.

=head2 config_id

This method returns the logger's configuration id, which defaults to its ident.
This can be used to make two loggers equivalent in Log::Dispatchouli::Global so
that trying to reinitialize with a new logger with the same C<config_id> as the
current logger will not throw an exception, and will simply do no thing.

=head2 dispatcher

This returns the underlying Log::Dispatch object.  This is not the method
you're looking for.  Move along.

=head2 stdio_dispatcher_class

This method is an experimental feature to allow you to pick an alternate
dispatch class for stderr and stdio.  By default, Log::Dispatch::Screen is
used.  B<This feature may go away at any time.>

=head1 LOGGER PREFIX

Log messages may be prepended with information to set context.  This can be set
at a logger level or per log item.  The simplest example is:

  my $logger = Log::Dispatchouli->new( ... );

  $logger->set_prefix("Batch 123: ");

  $logger->log("begun processing");

  # ...

  $logger->log("finished processing");

The above will log something like:

  Batch 123: begun processing
  Batch 123: finished processing

To pass a prefix per-message:

  $logger->log({ prefix => 'Sub-Item 234: ' }, 'error!')

  # Logs: Batch 123: Sub-Item 234: error!

If the prefix is a string, it is prepended to each line of the message.  If it
is a coderef, it is called and passed the message to be logged.  The return
value is logged instead.

L<Proxy loggers|/METHODS FOR PROXY LOGGERS> also have their own prefix
settings, which accumulate.  So:

  my $proxy = $logger->proxy({ proxy_prefix => 'Subsystem 12: ' });

  $proxy->set_prefix('Page 9: ');

  $proxy->log({ prefix => 'Paragraph 6: ' }, 'Done.');

...will log...

  Batch 123: Subsystem 12: Page 9: Paragraph 6: Done.

=head1 METHODS FOR SUBCLASSING

=head2 string_flogger

This method returns the thing on which F<flog> will be called to format log
messages.  By default, it just returns C<String::Flogger>

=head2 env_prefix

This method should return a string used as a prefix to find environment
variables that affect the logger's behavior.  For example, if this method
returns C<XYZZY> then when checking the environment for a default value for the
C<debug> parameter, Log::Dispatchouli will first check C<XYZZY_DEBUG>, then
C<DISPATCHOULI_DEBUG>.

By default, this method returns C<()>, which means no extra environment
variable is checked.

=head2 env_value

  my $value = $logger->env_value('DEBUG');

This method returns the value for the environment variable suffix given.  For
example, the example given, calling with C<DEBUG> will check
C<DISPATCHOULI_DEBUG>.

=head1 METHODS FOR TESTING

=head2 new_tester

  my $logger = Log::Dispatchouli->new_tester( \%arg );

This returns a new logger that logs only C<to_self>.  It's useful in testing.
If no C<ident> arg is provided, one will be generated.  C<log_pid> is off by
default, but can be overridden.

C<\%arg> is optional.

=head2 events

This method returns the arrayref of events logged to an array in memory (in the
logger).  If the logger is not logging C<to_self> this raises an exception.

=head2 clear_events

This method empties the current sequence of events logged into an array in
memory.  If the logger is not logging C<to_self> this raises an exception.

=head1 METHODS FOR PROXY LOGGERS

=head2 proxy

  my $proxy_logger = $logger->proxy( \%arg );

This method returns a new proxy logger -- an instance of
L<Log::Dispatchouli::Proxy> -- which will log through the given logger, but
which may have some settings localized.

C<%arg> is optional.  It may contain the following entries:

=over 4

=item proxy_prefix

This is a prefix that will be applied to anything the proxy logger logs, and
cannot be changed.

=item proxy_ctx

This is data to be inserted in front of event data logged through the proxy.
It will appear I<after> the C<event> key but before the logged event data.  It
can be in the same format as the C<$data_ref> argument to C<log_event>.

=item debug

This can be set to true or false to change the proxy's "am I in debug mode?"
setting.  It can be changed or cleared later on the proxy.

=back

=head2 parent

=head2 logger

These methods return the logger itself.  (They're more useful when called on
proxy loggers.)

=head1 METHODS FOR API COMPATIBILITY

To provide compatibility with some other loggers, most specifically
L<Log::Contextual>, the following methods are provided.  You should not use
these methods without a good reason, and you should never subclass them.
Instead, subclass the methods they call.

=over 4

=item is_debug

This method calls C<get_debug>.

=item is_info

=item is_fatal

These methods return true.

=item info

=item fatal

=item debug

These methods redispatch to C<log>, C<log_fatal>, and C<log_debug>
respectively.

=back

=head1 SEE ALSO

=over 4

=item *

L<Log::Dispatch>

=item *

L<String::Flogger>

=back

=head1 AUTHOR

Ricardo SIGNES <cpan@semiotic.systems>

=head1 CONTRIBUTORS

=for stopwords Charlie Garrison Christopher J. Madsen Dagfinn Ilmari Mannsåker Dan Book George Hartzell Jon Stuart Matt Phillips Olivier Mengué Randy Stauner Ricardo Signes Sawyer X

=over 4

=item *

Charlie Garrison <cng@garrison.com.au>

=item *

Christopher J. Madsen <perl@cjmweb.net>

=item *

Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>

=item *

Dan Book <grinnz@gmail.com>

=item *

George Hartzell <hartzell@alerce.com>

=item *

Jon Stuart <jon@fastmailteam.com>

=item *

Matt Phillips <mattp@cpan.org>

=item *

Olivier Mengué <dolmen@cpan.org>

=item *

Randy Stauner <randy@magnificent-tears.com>

=item *

Ricardo Signes <rjbs@semiotic.systems>

=item *

Sawyer X <xsawyerx@cpan.org>

=back

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2025 by Ricardo SIGNES.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut