File: events.t

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 (399 lines) | stat: -rw-r--r-- 10,217 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
use strict;
use warnings;

use experimental 'postderef'; # Not dangerous.  Is accepted without changed.

use utf8;

use JSON::MaybeXS;
use Log::Dispatchouli;
use Test::More 0.88;
use Test::Deep;

sub event_logs_ok {
  my ($event_type, $data, $line, $desc) = @_;

  local $Test::Builder::Level = $Test::Builder::Level+1;

  my $logger = Log::Dispatchouli->new_tester({
    log_pid => 0,
    ident   => 't/basic.t',
  });

  $logger->log_event($event_type, $data);

  messages_ok($logger, [$line], $desc);
}

sub messages_ok {
  my ($logger, $lines, $desc) = @_;

  local $Test::Builder::Level = $Test::Builder::Level+1;

  my @messages = map {; $_->{message} } $logger->events->@*;

  my $ok = cmp_deeply(
    \@messages,
    $lines,
    $desc,
  );

  $logger->clear_events;

  unless ($ok) {
    diag "GOT: $_" for @messages;
  }

  return $ok;
}

sub parse_event_ok {
  my ($event_string, $expect, $desc) = @_;

  local $Test::Builder::Level = $Test::Builder::Level+1;

  my $result = Log::Fmt->parse_event_string($event_string);

  cmp_deeply(
    $result,
    $expect,
    $desc,
  ) or note explain $result;
}

sub logger_trio {
  my $logger = Log::Dispatchouli->new_tester({
    log_pid => 0,
    ident   => 't/basic.t',
  });

  my $proxy1 = $logger->proxy({ proxy_ctx => { 'outer' => 'proxy' } });
  my $proxy2 = $proxy1->proxy({ proxy_ctx => { 'inner' => 'proxy' } });

  return ($logger, $proxy1, $proxy2);
}

subtest "very basic stuff" => sub {
  event_logs_ok(
    'world-series' => [ phl => 1, hou => 0, games => [ 'done', 'in-progress' ] ],
    'event=world-series phl=1 hou=0 games.0=done games.1=in-progress',
    "basic data with an arrayref value",
  );

  parse_event_ok(
    'event=world-series phl=1 hou=0 games.0=done games.1=in-progress',
    [
      event => 'world-series',
      phl => 1,
      hou => 0,
      'games.0' => 'done',
      'games.1' => 'in-progress',
    ],
    'we can parse something we produced'
  );

  event_logs_ok(
    'programmer-sleepiness' => {
      weary   => 8.62,
      excited => 3.2,
      motto   => q{Never say "never" ever again.},
    },
    'event=programmer-sleepiness excited=3.2 motto="Never say \\"never\\" ever again." weary=8.62',
    "basic data as a hashref",
  );

  {
    my %kv = (
      weary   => 8.62,
      excited => 3.2,
      motto   => q{Never say "never" ever again.},
    );

    my $line = Log::Fmt->format_event_string([%kv]);

    cmp_deeply(
      Log::Fmt->parse_event_string_as_hash($line),
      \%kv,
      "parse_event_string_as_hash works",
    );
  }

  parse_event_ok(
    'event=programmer-sleepiness excited=3.2 motto="Never say \\"never\\" ever again." weary=8.62',
    [
      event   => 'programmer-sleepiness',
      excited => '3.2',
      motto   => q{Never say "never" ever again.},
      weary   => '8.62',
    ],
    "parse an event with simple quotes",
  );

  event_logs_ok(
    'rich-structure' => [
      array => [
        { name => [ qw(Ricardo Signes) ], limbs => { arms => 2, legs => 2 } },
        [ 2, 4, 6 ],
      ],
    ],
    join(q{ }, qw(
      event=rich-structure
      array.0.limbs.arms=2
      array.0.limbs.legs=2
      array.0.name.0=Ricardo
      array.0.name.1=Signes
      array.1.0=2
      array.1.1=4
      array.1.2=6
    )),
    "a structured nested a few levels",
  );

  event_logs_ok(
    'empty-key' => { '' => 'disgusting' },
    'event=empty-key ~=disgusting',
    "cope with jerks putting empty keys into the data structure",
  );

  event_logs_ok(
    'bogus-subkey' => { valid => { 'foo bar' => 'revolting' } },
    'event=bogus-subkey valid.foo?bar=revolting',
    "cope with bogus key characters in recursion",
  );

  event_logs_ok(
    ctrlctl => [ string => qq{NL \x0a CR \x0d "Q" ZWJ \x{200D} \\nothing ë}, ],
    'event=ctrlctl string="NL \\n CR \\r \\"Q\\" ZWJ \\x{200d} \\\\nothing ë"',
    'control characters and otherwise',
  );

  parse_event_ok(
    'event=ctrlctl string="NL \\n CR \\r \\"Q\\" ZWJ \\x{200d} \\\\nothing ë"',
    [
      event   => 'ctrlctl',
      string  => qq{NL \x0a CR \x0d "Q" ZWJ \x{200D} \\nothing ë},
    ],
    "parse an event with simple quotes",
  );

  event_logs_ok(
    spacey => [ string => qq{line \x{2028} spacer} ],
    'event=spacey string="line \x{2028} spacer"',
    'non-control non-ascii vertical whitespace is also escaped',
  );

  parse_event_ok(
    'event=spacey string="line \x{2028} spacer"',
    [
      event   => 'spacey',
      string  => qq{line \x{2028} spacer}
    ],
    "parse an that has an escaped vertical whitespace cahracter",
  );
};

subtest "very basic proxy operation" => sub {
  my ($logger, $proxy1, $proxy2) = logger_trio();

  $proxy2->log_event(pie_picnic => [
    pies_eaten => 1.2,
    joy_harvested => 6,
  ]);

  messages_ok(
    $logger,
    [
      'event=pie_picnic outer=proxy inner=proxy pies_eaten=1.2 joy_harvested=6'
    ],
    'got the expected log output from events',
  );
};

subtest "debugging in the proxies" => sub {
  my ($logger, $proxy1, $proxy2) = logger_trio();

  $proxy1->set_debug(1);

  $logger->log_debug_event(0 => [ seq => 0 ]);
  $proxy1->log_debug_event(1 => [ seq => 1 ]);
  $proxy2->log_debug_event(2 => [ seq => 2 ]);

  $proxy2->set_debug(0);

  $logger->log_debug_event(0 => [ seq => 3 ]);
  $proxy1->log_debug_event(1 => [ seq => 4 ]);
  $proxy2->log_debug_event(2 => [ seq => 5 ]);

  messages_ok(
    $logger,
    [
      # 'event=0 seq=0',                          # not logged, debugging
      'event=1 outer=proxy seq=1',
      'event=2 outer=proxy inner=proxy seq=2',
      # 'event=0 seq=3',                          # not logged, debugging
      'event=1 outer=proxy seq=4',
      # 'event=2 outer=proxy inner=proxy seq=5',  # not logged, debugging
    ],
    'got the expected log output from events',
  );
};

# NOT TESTED HERE:  "mute" and "unmute", which rjbs believes are probably
# broken already.  Their tests don't appear to test the important case of "root
# logger muted, proxy explicitly unmuted".

subtest "recursive structure" => sub {
  my ($logger, $proxy1, $proxy2) = logger_trio();

  my $struct = {};

  $struct->{recurse} = $struct;

  $logger->log_event('recursive-thing' => [ recursive => $struct ]);

  messages_ok(
    $logger,
    [
      'event=recursive-thing recursive.recurse=&recursive',
    ],
    "an event with recursive stuff terminates",
  );
};

subtest "lazy values" => sub {
  my ($logger) = logger_trio();

  my $called = 0;
  my $callback = sub { $called++; return 'X' };

  $logger->log_event('sub-caller' => [
    once  => $callback,
    twice => $callback,
  ]);

  $logger->log_debug_event('sub-caller' => [
    d_once  => $callback,
    d_twice => $callback,
  ]);

  messages_ok(
    $logger,
    [
      'event=sub-caller once=X twice=X',
    ],
    "we call sublike arguments to lazily compute",
  );

  is($called, 2, "only called twice; debug events did not call sub");
};

subtest "lazy values in proxy context" => sub {
  my ($logger) = logger_trio();

  my $called_A = 0;
  my $callback_A = sub { $called_A++; return 'X' };

  my $called_B = 0;
  my $callback_B = sub { $called_B++; return 'X' };

  my $proxy1 = $logger->proxy({ proxy_ctx => [ outer => $callback_A ] });
  my $proxy2 = $proxy1->proxy({ proxy_ctx => [ inner => $callback_B ] });

  $proxy1->log_event('outer-event' => [ guitar => 'electric' ]);

  is($called_A, 1, "outer proxy did log, called outer callback");
  is($called_B, 0, "outer proxy did log, didn't call inner callback");

  $proxy2->log_event('inner-event' => [ mandolin => 'bluegrass' ]);

  is($called_A, 1, "inner proxy did log, didn't re-call outer callback");
  is($called_B, 1, "inner proxy did log, did call inner callback");

  $proxy2->log_event('inner-second' => [ snare => 'infinite' ]);

  messages_ok(
    $logger,
    [
      'event=outer-event outer=X guitar=electric',
      'event=inner-event outer=X inner=X mandolin=bluegrass',
      'event=inner-second outer=X inner=X snare=infinite',
    ],
    "all our laziness didn't change our results",
  );
};

subtest "reused JSON booleans" => sub {
  # It's not that this is extremely special, but we mostly don't want to
  # recurse into the same reference value multiple times, but we also don't
  # want the infuriating "reused boolean variable" you get from Dumper.  This
  # is just to make sure I don't accidentally break this case.
  my ($logger, $proxy1, $proxy2) = logger_trio();

  my $struct = {
    b => [ JSON::MaybeXS::true(), JSON::MaybeXS::false() ],
    f => [ (JSON::MaybeXS::false()) x 3 ],
    t => [ (JSON::MaybeXS::true())  x 3 ],
  };

  $logger->log_event('tf-thing' => [ cond => $struct ]);

  messages_ok(
    $logger,
    [
      'event=tf-thing cond.b.0=1 cond.b.1=0 cond.f.0=0 cond.f.1=0 cond.f.2=0 cond.t.0=1 cond.t.1=1 cond.t.2=1',
    ],
    "JSON bools do what we expect",
  );
};

subtest "JSON-ification of refrefs" => sub {
  my ($logger, $proxy1, $proxy2) = logger_trio();

  $logger->log_event('json-demo' => [
    foo =>  { a => 1 },
    bar => \{ a => 1 },
    baz => \[ 12, 34 ],
  ]);

  my @messages = map {; $_->{message} } $logger->events->@*;

  messages_ok(
    $logger,
    [
      # XS and PP versions of JSON differ on space, so we need "12, 34" and
      # "12,34" both.  Then things get weird, because the version with no
      # spaces (pure perl, at least as of today) doesn't need to be quoted to
      # be used as a logfmt value, so the quotes are now optional.  Wild.
      # -- rjbs, 2023-09-02
      any(
        'event=json-demo foo.a=1 bar="{{{\"a\": 1}}}" baz="{{[12, 34]}}"',
        'event=json-demo foo.a=1 bar="{{{\"a\": 1}}}" baz={{[12,34]}}',
      ),
    ],
    "refref becomes JSON flogged",
  );

  my $result = Log::Fmt->parse_event_string($messages[0]);

  cmp_deeply(
    $result,
    [
      event   => 'json-demo',
      'foo.a' => 1,
      bar     => "{{{\"a\": 1}}}",
      baz     => any("{{[12, 34]}}", "{{[12,34]}}"),
    ],
    "parsing gets us JSON string out, because it is just strings",
  );

  my ($json_string) = $result->[5] =~ /\A\{\{(.+)\}\}\z/;
  my $json_struct = decode_json($json_string);

  cmp_deeply(
    $json_struct,
    { a => 1 },
    "we can round trip that JSON",
  );
};

done_testing;