@larablox/monolog 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +86 -0
  3. package/out/Monolog/Handler/AbstractProcessingHandler.d.ts +3 -1
  4. package/out/Monolog/Handler/FingersCrossed/ActivationStrategyInterface.d.ts +19 -0
  5. package/out/Monolog/Handler/FingersCrossed/ActivationStrategyInterface.luau +30 -0
  6. package/out/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.d.ts +29 -0
  7. package/out/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.luau +61 -0
  8. package/out/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.d.ts +14 -0
  9. package/out/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.luau +32 -0
  10. package/out/Monolog/Handler/FingersCrossedHandler.d.ts +107 -0
  11. package/out/Monolog/Handler/FingersCrossedHandler.luau +208 -0
  12. package/out/Monolog/Handler/FormattableHandlerInterface.d.ts +23 -0
  13. package/out/Monolog/Handler/FormattableHandlerInterface.luau +36 -0
  14. package/out/Monolog/Handler/GroupHandler.d.ts +49 -0
  15. package/out/Monolog/Handler/GroupHandler.luau +139 -0
  16. package/out/Monolog/Handler/HandlerInterface.d.ts +10 -0
  17. package/out/Monolog/Handler/HandlerInterface.luau +36 -1
  18. package/out/Monolog/Handler/ProcessableHandlerInterface.d.ts +26 -0
  19. package/out/Monolog/Handler/ProcessableHandlerInterface.luau +15 -0
  20. package/out/Monolog/Handler/RobloxConsoleHandler.d.ts +19 -46
  21. package/out/Monolog/Handler/RobloxConsoleHandler.luau +28 -99
  22. package/out/Monolog/Handler/WhatFailureGroupHandler.d.ts +26 -0
  23. package/out/Monolog/Handler/WhatFailureGroupHandler.luau +85 -0
  24. package/package.json +11 -3
package/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011-2020 Jordi Boggiano
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is furnished
8
+ to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Larablox Monolog - Logging for Roblox
2
+
3
+ A roblox-ts port of [Monolog](https://github.com/Seldaek/monolog), as
4
+ faithfully as the Roblox platform allows. Upstream Monolog sends logs to
5
+ files, sockets, inboxes, databases, and web services; this port ships only
6
+ what a Roblox place can actually back -- printing to the output console, and
7
+ discarding. See [`CLAUDE.md`](CLAUDE.md)'s "Not ported" section for the full,
8
+ maintained list of what didn't make the trip and why.
9
+
10
+ This package implements the same shape as PSR-3's
11
+ [`LoggerInterface`](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)
12
+ (`LoggerInterface.ts`), so code written against it stays interoperable with
13
+ the concept even without a real `psr/log` package on this platform.
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @larablox/monolog
19
+ ```
20
+
21
+ ## Basic Usage
22
+
23
+ ```ts
24
+ import { Logger } from "@larablox/monolog/out/Monolog/Logger";
25
+ import { RobloxConsoleHandler } from "@larablox/monolog/out/Monolog/Handler/RobloxConsoleHandler";
26
+ import { Level } from "@larablox/monolog/out/Monolog/Level";
27
+
28
+ // create a log channel
29
+ const log = new Logger("name");
30
+ log.pushHandler(new RobloxConsoleHandler(Level.Warning));
31
+
32
+ // add records to the log
33
+ log.warning("Foo");
34
+ log.error("Bar");
35
+ ```
36
+
37
+ There is no package-level barrel export -- `src/index.ts` explains why --
38
+ so consumers deep-import each class the way the example above does.
39
+
40
+ ## What's included
41
+
42
+ - Core: `Logger`, `LoggerInterface` (the PSR-3 shape), `Level`, `LogRecord`,
43
+ `Registry`, `Utils`, `ResettableInterface`
44
+ - Handlers: `NullHandler`, `RobloxConsoleHandler` (writes formatted records to
45
+ the Roblox output console), and `TestHandler` for your own tests
46
+ - Wrapping handlers: `GroupHandler`, `WhatFailureGroupHandler`,
47
+ `FingersCrossedHandler` (with `ErrorLevelActivationStrategy` and
48
+ `ChannelLevelActivationStrategy`)
49
+ - Handler base classes and interfaces, for writing your own: `Handler`,
50
+ `AbstractHandler`, `AbstractProcessingHandler`, `HandlerInterface`,
51
+ `FormattableHandlerInterface`, `ProcessableHandlerInterface`
52
+ - Formatters: `NormalizerFormatter`, `LineFormatter`, `JsonFormatter`,
53
+ `ScalarFormatter`, `HtmlFormatter`, and `FormatterInterface`
54
+ - Processors: `PsrLogMessageProcessor`, `ClosureContextProcessor`,
55
+ `IntrospectionProcessor`, `MemoryUsageProcessor`, `MemoryPeakUsageProcessor`,
56
+ `ProcessIdProcessor`, `TagProcessor`, `UidProcessor`, and
57
+ `ProcessorInterface`
58
+ - Attributes: `WithMonologChannel`, `AsMonologProcessor`
59
+ - Test helpers: `Test/MonologTestCase` (plus `Test/TestCase`, upstream's
60
+ deprecated alias for it), shipped in the published package the same way
61
+ upstream ships them, because consumers use them in their own suites
62
+
63
+ ## Submitting bugs and feature requests
64
+
65
+ Bugs and feature requests are tracked on
66
+ [GitHub](https://github.com/larablox/monolog/issues).
67
+
68
+ ## Requirements
69
+
70
+ - TypeScript 5.x compiled with [roblox-ts](https://roblox-ts.com/) `^3.0`
71
+ - A Rojo-synced Roblox place to run the compiled output in
72
+
73
+ ## Framework Integration
74
+
75
+ [`larablox/framework`](https://github.com/larablox/framework)'s
76
+ `Illuminate\Log` is built on this package.
77
+
78
+ ## License
79
+
80
+ MIT, matching upstream Monolog.
81
+
82
+ ## Acknowledgements
83
+
84
+ This is a TypeScript/roblox-ts port of [Monolog](https://github.com/Seldaek/monolog)
85
+ by Jordi Boggiano, adapted to run on the Roblox platform as faithfully as it
86
+ allows.
@@ -1,12 +1,14 @@
1
1
  import { AbstractHandler } from "./AbstractHandler";
2
+ import type { FormattableHandlerInterface } from "./FormattableHandlerInterface";
2
3
  import type { FormatterInterface } from "../Formatter/FormatterInterface";
3
4
  import type { LogRecord } from "../LogRecord";
5
+ import type { ProcessableHandlerInterface } from "./ProcessableHandlerInterface";
4
6
  import type { Processor } from "../Processor/ProcessorInterface";
5
7
  /**
6
8
  * PHP: `Monolog\Handler\AbstractProcessingHandler`, which folds in
7
9
  * `ProcessableHandlerTrait` and `FormattableHandlerTrait`.
8
10
  */
9
- export declare abstract class AbstractProcessingHandler extends AbstractHandler {
11
+ export declare abstract class AbstractProcessingHandler extends AbstractHandler implements ProcessableHandlerInterface, FormattableHandlerInterface {
10
12
  protected processors: Processor[];
11
13
  protected formatter?: FormatterInterface;
12
14
  /** Handles a record. */
@@ -0,0 +1,19 @@
1
+ import type { LogRecord } from "../../LogRecord";
2
+ /**
3
+ * PHP: `Monolog\Handler\FingersCrossed\ActivationStrategyInterface`.
4
+ *
5
+ * Interface for activation strategies for the `FingersCrossedHandler`.
6
+ */
7
+ export interface ActivationStrategyInterface {
8
+ /** Returns whether the given record activates the handler. */
9
+ isHandlerActivated(record: LogRecord): boolean;
10
+ }
11
+ /**
12
+ * True when the given value implements `ActivationStrategyInterface`.
13
+ *
14
+ * `FingersCrossedHandler`'s constructor takes either a strategy or a bare
15
+ * level and branches on `instanceof`; there is no `instanceof` for an
16
+ * interface here, so this checks structurally for the one callable member, as
17
+ * `isResettable()` (`ResettableInterface.ts`) already does for its own.
18
+ */
19
+ export declare function isActivationStrategy(value: unknown): value is ActivationStrategyInterface;
@@ -0,0 +1,30 @@
1
+ -- Compiled with roblox-ts v3.0.0
2
+ --[[
3
+ *
4
+ * PHP: `Monolog\Handler\FingersCrossed\ActivationStrategyInterface`.
5
+ *
6
+ * Interface for activation strategies for the `FingersCrossedHandler`.
7
+
8
+ ]]
9
+ --[[
10
+ *
11
+ * True when the given value implements `ActivationStrategyInterface`.
12
+ *
13
+ * `FingersCrossedHandler`'s constructor takes either a strategy or a bare
14
+ * level and branches on `instanceof`; there is no `instanceof` for an
15
+ * interface here, so this checks structurally for the one callable member, as
16
+ * `isResettable()` (`ResettableInterface.ts`) already does for its own.
17
+
18
+ ]]
19
+ local function isActivationStrategy(value)
20
+ local _value = value
21
+ if not (type(_value) == "table") then
22
+ return false
23
+ end
24
+ local candidate = value
25
+ local _isHandlerActivated = candidate.isHandlerActivated
26
+ return type(_isHandlerActivated) == "function"
27
+ end
28
+ return {
29
+ isActivationStrategy = isActivationStrategy,
30
+ }
@@ -0,0 +1,29 @@
1
+ import type { ActivationStrategyInterface } from "./ActivationStrategyInterface";
2
+ import type { Level } from "../../Level";
3
+ import type { LogLevel } from "../../LoggerInterface";
4
+ import type { LogRecord } from "../../LogRecord";
5
+ /**
6
+ * PHP: `Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy`.
7
+ *
8
+ * Channel and error level based activation strategy. Allows triggering
9
+ * activation based on level per channel: e.g. trigger activation on level
10
+ * `Error` by default, except for records of the `sql` channel, which should
11
+ * trigger activation on level `Warning`.
12
+ *
13
+ * ```ts
14
+ * const activationStrategy = new ChannelLevelActivationStrategy(
15
+ * Level.Critical,
16
+ * { request: Level.Alert, sensitive: Level.Error },
17
+ * );
18
+ * const handler = new FingersCrossedHandler(
19
+ * new RobloxConsoleHandler(),
20
+ * activationStrategy,
21
+ * );
22
+ * ```
23
+ */
24
+ export declare class ChannelLevelActivationStrategy implements ActivationStrategyInterface {
25
+ private readonly defaultActionLevel;
26
+ private readonly channelToActionLevel;
27
+ constructor(defaultActionLevel: Level | LogLevel, channelToActionLevel?: Record<string, Level | LogLevel>);
28
+ isHandlerActivated(record: LogRecord): boolean;
29
+ }
@@ -0,0 +1,61 @@
1
+ -- Compiled with roblox-ts v3.0.0
2
+ local TS = _G[script]
3
+ local Logger = TS.import(script, script.Parent.Parent.Parent, "Logger").Logger
4
+ --[[
5
+ *
6
+ * PHP: `Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy`.
7
+ *
8
+ * Channel and error level based activation strategy. Allows triggering
9
+ * activation based on level per channel: e.g. trigger activation on level
10
+ * `Error` by default, except for records of the `sql` channel, which should
11
+ * trigger activation on level `Warning`.
12
+ *
13
+ * ```ts
14
+ * const activationStrategy = new ChannelLevelActivationStrategy(
15
+ * Level.Critical,
16
+ * { request: Level.Alert, sensitive: Level.Error },
17
+ * );
18
+ * const handler = new FingersCrossedHandler(
19
+ * new RobloxConsoleHandler(),
20
+ * activationStrategy,
21
+ * );
22
+ * ```
23
+
24
+ ]]
25
+ local ChannelLevelActivationStrategy
26
+ do
27
+ ChannelLevelActivationStrategy = setmetatable({}, {
28
+ __tostring = function()
29
+ return "ChannelLevelActivationStrategy"
30
+ end,
31
+ })
32
+ ChannelLevelActivationStrategy.__index = ChannelLevelActivationStrategy
33
+ function ChannelLevelActivationStrategy.new(...)
34
+ local self = setmetatable({}, ChannelLevelActivationStrategy)
35
+ return self:constructor(...) or self
36
+ end
37
+ function ChannelLevelActivationStrategy:constructor(defaultActionLevel, channelToActionLevel)
38
+ if channelToActionLevel == nil then
39
+ channelToActionLevel = {}
40
+ end
41
+ self.channelToActionLevel = {}
42
+ self.defaultActionLevel = Logger:toMonologLevel(defaultActionLevel)
43
+ for channel, level in pairs(channelToActionLevel) do
44
+ local _channelToActionLevel = self.channelToActionLevel
45
+ local _arg1 = Logger:toMonologLevel(level)
46
+ _channelToActionLevel[channel] = _arg1
47
+ end
48
+ end
49
+ function ChannelLevelActivationStrategy:isHandlerActivated(record)
50
+ local _channelToActionLevel = self.channelToActionLevel
51
+ local _channel = record.channel
52
+ local channelLevel = _channelToActionLevel[_channel]
53
+ if channelLevel ~= nil then
54
+ return record.level >= channelLevel
55
+ end
56
+ return record.level >= self.defaultActionLevel
57
+ end
58
+ end
59
+ return {
60
+ ChannelLevelActivationStrategy = ChannelLevelActivationStrategy,
61
+ }
@@ -0,0 +1,14 @@
1
+ import type { ActivationStrategyInterface } from "./ActivationStrategyInterface";
2
+ import type { Level } from "../../Level";
3
+ import type { LogLevel } from "../../LoggerInterface";
4
+ import type { LogRecord } from "../../LogRecord";
5
+ /**
6
+ * PHP: `Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy`.
7
+ *
8
+ * Error level based activation strategy.
9
+ */
10
+ export declare class ErrorLevelActivationStrategy implements ActivationStrategyInterface {
11
+ private readonly actionLevel;
12
+ constructor(actionLevel: Level | LogLevel);
13
+ isHandlerActivated(record: LogRecord): boolean;
14
+ }
@@ -0,0 +1,32 @@
1
+ -- Compiled with roblox-ts v3.0.0
2
+ local TS = _G[script]
3
+ local Logger = TS.import(script, script.Parent.Parent.Parent, "Logger").Logger
4
+ --[[
5
+ *
6
+ * PHP: `Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy`.
7
+ *
8
+ * Error level based activation strategy.
9
+
10
+ ]]
11
+ local ErrorLevelActivationStrategy
12
+ do
13
+ ErrorLevelActivationStrategy = setmetatable({}, {
14
+ __tostring = function()
15
+ return "ErrorLevelActivationStrategy"
16
+ end,
17
+ })
18
+ ErrorLevelActivationStrategy.__index = ErrorLevelActivationStrategy
19
+ function ErrorLevelActivationStrategy.new(...)
20
+ local self = setmetatable({}, ErrorLevelActivationStrategy)
21
+ return self:constructor(...) or self
22
+ end
23
+ function ErrorLevelActivationStrategy:constructor(actionLevel)
24
+ self.actionLevel = Logger:toMonologLevel(actionLevel)
25
+ end
26
+ function ErrorLevelActivationStrategy:isHandlerActivated(record)
27
+ return record.level >= self.actionLevel
28
+ end
29
+ end
30
+ return {
31
+ ErrorLevelActivationStrategy = ErrorLevelActivationStrategy,
32
+ }
@@ -0,0 +1,107 @@
1
+ import { Handler } from "./Handler";
2
+ import { Level } from "../Level";
3
+ import type { ActivationStrategyInterface } from "./FingersCrossed/ActivationStrategyInterface";
4
+ import type { FormattableHandlerInterface } from "./FormattableHandlerInterface";
5
+ import type { FormatterInterface } from "../Formatter/FormatterInterface";
6
+ import type { HandlerInterface } from "./HandlerInterface";
7
+ import type { LogLevel } from "../LoggerInterface";
8
+ import type { LogRecord } from "../LogRecord";
9
+ import type { Processor } from "../Processor/ProcessorInterface";
10
+ import type { ProcessableHandlerInterface } from "./ProcessableHandlerInterface";
11
+ import type { ResettableInterface } from "../ResettableInterface";
12
+ /**
13
+ * PHP: the `Closure(LogRecord|null, HandlerInterface): HandlerInterface`
14
+ * factory `FingersCrossedHandler` accepts in place of a handler.
15
+ */
16
+ export type HandlerFactory = (record: LogRecord | undefined, handler: FingersCrossedHandler) => HandlerInterface;
17
+ /**
18
+ * PHP: `Monolog\Handler\FingersCrossedHandler`.
19
+ *
20
+ * Buffers all records until a certain level is reached.
21
+ *
22
+ * The advantage of this approach is that you get no clutter in your log. Only
23
+ * sessions which actually trigger an error (or whatever the
24
+ * `activationStrategy` is) end up in the log, but they contain all records,
25
+ * not only those above the level threshold.
26
+ *
27
+ * There is a `passthruLevel` as well, which means that at the end, even if the
28
+ * handler never got activated, it will still send through log records of at
29
+ * least that level.
30
+ *
31
+ * Upstream gets its processor stack from `ProcessableHandlerTrait`; Luau has
32
+ * no traits, so the trait's members are spelled out here -- see the same note
33
+ * on `GroupHandler` and `AbstractProcessingHandler`.
34
+ */
35
+ export declare class FingersCrossedHandler extends Handler implements ProcessableHandlerInterface, ResettableInterface, FormattableHandlerInterface {
36
+ protected processors: Processor[];
37
+ /** The nested handler, or the factory that will produce it on first use. */
38
+ protected handler: HandlerInterface | HandlerFactory;
39
+ protected activationStrategy: ActivationStrategyInterface;
40
+ protected buffering: boolean;
41
+ protected bufferSize: number;
42
+ protected buffer: LogRecord[];
43
+ protected stopBuffering: boolean;
44
+ protected passthruLevel?: Level;
45
+ protected bubble: boolean;
46
+ /**
47
+ * @param handler Handler or factory `(record | undefined, fingersCrossedHandler)`.
48
+ * @param activationStrategy Strategy determining when this handler takes
49
+ * action, or a level (name or `Level`) at which it is activated.
50
+ * @param bufferSize How many entries to buffer at most; beyond that the
51
+ * oldest items are removed from the buffer. `0` means unbounded.
52
+ * @param bubble Whether the handled messages can bubble up the stack.
53
+ * @param stopBuffering Whether to stop buffering after being triggered.
54
+ * @param passthruLevel Minimum level to always flush to the nested handler
55
+ * on close, even if the strategy never triggered.
56
+ */
57
+ constructor(handler: HandlerInterface | HandlerFactory, activationStrategy?: Level | LogLevel | ActivationStrategyInterface, bufferSize?: number, bubble?: boolean, stopBuffering?: boolean, passthruLevel?: Level | LogLevel);
58
+ /** Always true -- this handler decides what to do with a record later. */
59
+ isHandling(_record: LogRecord): boolean;
60
+ /** Manually activates this handler regardless of the activation strategy. */
61
+ activate(): void;
62
+ /** Handles a record, buffering it until the strategy activates. */
63
+ handle(record: LogRecord): boolean;
64
+ /** Flushes anything left at the passthru level, then closes the nested handler. */
65
+ close(): void;
66
+ /** Resets this handler, its processors, and the nested handler. */
67
+ reset(): void;
68
+ /**
69
+ * Clears the buffer without flushing any messages down to the wrapped
70
+ * handler. Also resets the handler to its initial buffering state.
71
+ */
72
+ clear(): void;
73
+ /**
74
+ * Resets the state of the handler. Stops forwarding records to the wrapped
75
+ * handler.
76
+ */
77
+ private flushBuffer;
78
+ /**
79
+ * Returns the nested handler.
80
+ *
81
+ * If the handler was provided as a factory, this triggers the handler's
82
+ * instantiation.
83
+ *
84
+ * Upstream's `\RuntimeException` for a factory that returns a non-handler
85
+ * is kept -- there is no exception class hierarchy on this platform, so it
86
+ * is a plain `error()` carrying upstream's message.
87
+ */
88
+ getHandler(record?: LogRecord): HandlerInterface;
89
+ /** Adds a processor in the stack. */
90
+ pushProcessor(processor: Processor): this;
91
+ /** Removes the processor on top of the stack and returns it. */
92
+ popProcessor(): Processor | undefined;
93
+ /** Processes a record. */
94
+ protected processRecord(record: LogRecord): LogRecord;
95
+ /** Resets any processors on this handler that support resetting. */
96
+ protected resetProcessors(): void;
97
+ /**
98
+ * Sets the formatter on the nested handler.
99
+ *
100
+ * Upstream's `\UnexpectedValueException` names the offending class with
101
+ * `get_class()`; `Utils.getClass()` is not ported (see `Utils.ts`), so the
102
+ * name comes from the roblox-ts class metatable's `__tostring` instead.
103
+ */
104
+ setFormatter(formatter: FormatterInterface): this;
105
+ /** Gets the formatter of the nested handler. */
106
+ getFormatter(): FormatterInterface;
107
+ }
@@ -0,0 +1,208 @@
1
+ -- Compiled with roblox-ts v3.0.0
2
+ local TS = _G[script]
3
+ local ErrorLevelActivationStrategy = TS.import(script, script.Parent, "FingersCrossed", "ErrorLevelActivationStrategy").ErrorLevelActivationStrategy
4
+ local Handler = TS.import(script, script.Parent, "Handler").Handler
5
+ local isActivationStrategy = TS.import(script, script.Parent, "FingersCrossed", "ActivationStrategyInterface").isActivationStrategy
6
+ local isFormattable = TS.import(script, script.Parent, "FormattableHandlerInterface").isFormattable
7
+ local isHandler = TS.import(script, script.Parent, "HandlerInterface").isHandler
8
+ local isResettable = TS.import(script, script.Parent.Parent, "ResettableInterface").isResettable
9
+ local _Level = TS.import(script, script.Parent.Parent, "Level")
10
+ local Level = _Level.Level
11
+ local Levels = _Level.Levels
12
+ local Logger = TS.import(script, script.Parent.Parent, "Logger").Logger
13
+ local runProcessor = TS.import(script, script.Parent.Parent, "Processor", "ProcessorInterface").runProcessor
14
+ --[[
15
+ *
16
+ * PHP: the `Closure(LogRecord|null, HandlerInterface): HandlerInterface`
17
+ * factory `FingersCrossedHandler` accepts in place of a handler.
18
+
19
+ ]]
20
+ --[[
21
+ *
22
+ * PHP: `Monolog\Handler\FingersCrossedHandler`.
23
+ *
24
+ * Buffers all records until a certain level is reached.
25
+ *
26
+ * The advantage of this approach is that you get no clutter in your log. Only
27
+ * sessions which actually trigger an error (or whatever the
28
+ * `activationStrategy` is) end up in the log, but they contain all records,
29
+ * not only those above the level threshold.
30
+ *
31
+ * There is a `passthruLevel` as well, which means that at the end, even if the
32
+ * handler never got activated, it will still send through log records of at
33
+ * least that level.
34
+ *
35
+ * Upstream gets its processor stack from `ProcessableHandlerTrait`; Luau has
36
+ * no traits, so the trait's members are spelled out here -- see the same note
37
+ * on `GroupHandler` and `AbstractProcessingHandler`.
38
+
39
+ ]]
40
+ local FingersCrossedHandler
41
+ do
42
+ local super = Handler
43
+ FingersCrossedHandler = setmetatable({}, {
44
+ __tostring = function()
45
+ return "FingersCrossedHandler"
46
+ end,
47
+ __index = super,
48
+ })
49
+ FingersCrossedHandler.__index = FingersCrossedHandler
50
+ function FingersCrossedHandler.new(...)
51
+ local self = setmetatable({}, FingersCrossedHandler)
52
+ return self:constructor(...) or self
53
+ end
54
+ function FingersCrossedHandler:constructor(handler, activationStrategy, bufferSize, bubble, stopBuffering, passthruLevel)
55
+ if bufferSize == nil then
56
+ bufferSize = 0
57
+ end
58
+ if bubble == nil then
59
+ bubble = true
60
+ end
61
+ if stopBuffering == nil then
62
+ stopBuffering = true
63
+ end
64
+ super.constructor(self)
65
+ self.processors = {}
66
+ self.buffering = true
67
+ self.buffer = {}
68
+ local strategy = activationStrategy
69
+ if strategy == nil then
70
+ strategy = ErrorLevelActivationStrategy.new(Level.Warning)
71
+ end
72
+ -- Convert a bare level activationStrategy to an object.
73
+ if not isActivationStrategy(strategy) then
74
+ strategy = ErrorLevelActivationStrategy.new(strategy)
75
+ end
76
+ self.handler = handler
77
+ self.activationStrategy = strategy
78
+ self.bufferSize = bufferSize
79
+ self.bubble = bubble
80
+ self.stopBuffering = stopBuffering
81
+ if passthruLevel ~= nil then
82
+ self.passthruLevel = Logger:toMonologLevel(passthruLevel)
83
+ end
84
+ end
85
+ function FingersCrossedHandler:isHandling(_record)
86
+ return true
87
+ end
88
+ function FingersCrossedHandler:activate()
89
+ if self.stopBuffering then
90
+ self.buffering = false
91
+ end
92
+ self:getHandler(self.buffer[#self.buffer]):handleBatch(self.buffer)
93
+ self.buffer = {}
94
+ end
95
+ function FingersCrossedHandler:handle(record)
96
+ local processed = record
97
+ if not (#self.processors == 0) then
98
+ processed = self:processRecord(processed)
99
+ end
100
+ if self.buffering then
101
+ local _buffer = self.buffer
102
+ local _processed = processed
103
+ table.insert(_buffer, _processed)
104
+ if self.bufferSize > 0 and #self.buffer > self.bufferSize then
105
+ table.remove(self.buffer, 1)
106
+ end
107
+ if self.activationStrategy:isHandlerActivated(processed) then
108
+ self:activate()
109
+ end
110
+ else
111
+ self:getHandler(processed):handle(processed)
112
+ end
113
+ return self.bubble == false
114
+ end
115
+ function FingersCrossedHandler:close()
116
+ self:flushBuffer()
117
+ self:getHandler():close()
118
+ end
119
+ function FingersCrossedHandler:reset()
120
+ self:flushBuffer()
121
+ self:resetProcessors()
122
+ local handler = self:getHandler()
123
+ if isResettable(handler) then
124
+ handler:reset()
125
+ end
126
+ end
127
+ function FingersCrossedHandler:clear()
128
+ self.buffer = {}
129
+ self:reset()
130
+ end
131
+ function FingersCrossedHandler:flushBuffer()
132
+ local passthruLevel = self.passthruLevel
133
+ if passthruLevel ~= nil then
134
+ local _exp = self.buffer
135
+ -- ▼ ReadonlyArray.filter ▼
136
+ local _newValue = {}
137
+ local _callback = function(record)
138
+ return Levels:includes(passthruLevel, record.level)
139
+ end
140
+ local _length = 0
141
+ for _k, _v in _exp do
142
+ if _callback(_v, _k - 1, _exp) == true then
143
+ _length += 1
144
+ _newValue[_length] = _v
145
+ end
146
+ end
147
+ -- ▲ ReadonlyArray.filter ▲
148
+ self.buffer = _newValue
149
+ if not (#self.buffer == 0) then
150
+ self:getHandler(self.buffer[#self.buffer]):handleBatch(self.buffer)
151
+ end
152
+ end
153
+ self.buffer = {}
154
+ self.buffering = true
155
+ end
156
+ function FingersCrossedHandler:getHandler(record)
157
+ local _handler = self.handler
158
+ if type(_handler) == "function" then
159
+ local produced = self.handler(record, self)
160
+ if not isHandler(produced) then
161
+ error("The factory Closure should return a HandlerInterface")
162
+ end
163
+ self.handler = produced
164
+ end
165
+ return self.handler
166
+ end
167
+ function FingersCrossedHandler:pushProcessor(processor)
168
+ local _processors = self.processors
169
+ local _processor = processor
170
+ table.insert(_processors, 1, _processor)
171
+ return self
172
+ end
173
+ function FingersCrossedHandler:popProcessor()
174
+ return table.remove(self.processors, 1)
175
+ end
176
+ function FingersCrossedHandler:processRecord(record)
177
+ local processed = record
178
+ for _, processor in self.processors do
179
+ processed = runProcessor(processor, processed)
180
+ end
181
+ return processed
182
+ end
183
+ function FingersCrossedHandler:resetProcessors()
184
+ for _, processor in self.processors do
185
+ if isResettable(processor) then
186
+ processor:reset()
187
+ end
188
+ end
189
+ end
190
+ function FingersCrossedHandler:setFormatter(formatter)
191
+ local handler = self:getHandler()
192
+ if isFormattable(handler) then
193
+ handler:setFormatter(formatter)
194
+ return self
195
+ end
196
+ return error(`The nested handler of type {tostring((getmetatable(handler)))} does not support formatters.`)
197
+ end
198
+ function FingersCrossedHandler:getFormatter()
199
+ local handler = self:getHandler()
200
+ if isFormattable(handler) then
201
+ return handler:getFormatter()
202
+ end
203
+ return error(`The nested handler of type {tostring((getmetatable(handler)))} does not support formatters.`)
204
+ end
205
+ end
206
+ return {
207
+ FingersCrossedHandler = FingersCrossedHandler,
208
+ }
@@ -0,0 +1,23 @@
1
+ import type { FormatterInterface } from "../Formatter/FormatterInterface";
2
+ import type { HandlerInterface } from "./HandlerInterface";
3
+ /**
4
+ * PHP: `Monolog\Handler\FormattableHandlerInterface`.
5
+ *
6
+ * Upstream pairs this with `FormattableHandlerTrait`; Luau has no traits, so
7
+ * implementers carry the `$formatter` field and `getDefaultFormatter()`
8
+ * themselves -- see `AbstractProcessingHandler`.
9
+ */
10
+ export interface FormattableHandlerInterface extends HandlerInterface {
11
+ /** Sets the formatter. */
12
+ setFormatter(formatter: FormatterInterface): this;
13
+ /** Gets the formatter. */
14
+ getFormatter(): FormatterInterface;
15
+ }
16
+ /**
17
+ * True when the given value implements `FormattableHandlerInterface`.
18
+ *
19
+ * PHP checks this with `instanceof`; there is no `instanceof` for an
20
+ * interface here, so this checks structurally for the two callable members,
21
+ * exactly as `isResettable()` (`ResettableInterface.ts`) already does.
22
+ */
23
+ export declare function isFormattable(value: unknown): value is FormattableHandlerInterface;