|
1 | | -## Terminal output and logging |
| 1 | +# Console |
2 | 2 |
|
3 | | -With a similar API to the `console` object provided by web browsers, the |
4 | | -[Console][] class[^1] provides: |
| 3 | +The Console component provides terminal output and message logging via an API |
| 4 | +similar to `console` in web browsers. |
5 | 5 |
|
6 | | -- Familiar methods like `Console::log()` and `Console::error()` |
7 | | -- Variants like `Console::logOnce()` and `Console::errorOnce()` to output |
8 | | - messages once per run |
| 6 | +- Familiar methods like [Console::log()][log()] and [Console::error()][error()] |
| 7 | +- Variants like [Console::logOnce()][logOnce()] and |
| 8 | + [Console::errorOnce()][errorOnce()] to output messages once per run |
9 | 9 | - Output to multiple targets |
10 | 10 | - Messages filtered by log level |
11 | | -- Formatting to reflect message priority and improve readability |
| 11 | +- Formatting that reflects message priority and improves readability |
| 12 | +- Optional Markdown-like syntax for additional formatting |
12 | 13 | - Colour output to TTYs |
| 14 | +- [PSR-3 (Logger Interface)][PSR-3] support via [Console::logger()][logger()] |
13 | 15 |
|
14 | | -### Default targets |
| 16 | +## Default targets |
15 | 17 |
|
16 | | -By default, [Console][] output is appended to a file in the default temporary |
17 | | -directory, created with mode `0600` if it doesn't already exist: |
| 18 | +If no targets are registered to receive [Console][] output when a message is |
| 19 | +written: |
18 | 20 |
|
19 | | -```php |
20 | | -<?php |
21 | | -sys_get_temp_dir() . '/<script_basename>-<realpath_hash>-<user_id>.log' |
22 | | -``` |
23 | | - |
24 | | -If the value of environment variable `console_target` is `stderr` or `stdout`, |
25 | | -[Console][] output is also written to `STDERR` or `STDOUT` respectively. |
| 21 | +1. Output is appended to a log file in PHP's default temporary directory, |
| 22 | + created with mode `0600` if it doesn't already exist: |
26 | 23 |
|
27 | | -If `console_target` is not set and the script is running on the command line: |
| 24 | + ```php |
| 25 | + sys_get_temp_dir() . '/<script_basename>-<realpath_hash>-<user_id>.log' |
| 26 | + ``` |
28 | 27 |
|
29 | | -- If `STDERR` is a TTY and `STDOUT` is not, console messages are written to |
30 | | - `STDERR` so output to `STDOUT` isn't tainted |
31 | | -- Otherwise, errors and warnings are written to `STDERR`, and informational |
32 | | - messages are written to `STDOUT` |
| 28 | +2. If the script is running on the command line, output is also written to |
| 29 | + `STDERR`. |
33 | 30 |
|
34 | | -Debug messages are written to the output log but are not written to `STDOUT` or |
35 | | -`STDERR` if environment variable `DEBUG` is empty or not set. |
| 31 | +Debug messages are suppressed unless debug mode is enabled in the environment, |
| 32 | +e.g. by setting `DEBUG=1`. |
36 | 33 |
|
37 | | -To override these defaults, register at least one [Console][] output target, |
38 | | -e.g. by calling [registerTarget()][], before any other `Console` methods are |
39 | | -called, preferably while bootstrapping your application. |
| 34 | +To override these defaults: |
40 | 35 |
|
41 | | -> [Application][] and [CliApplication][] always call [registerStderrTarget()][], |
42 | | -> which registers the default `STDOUT` and `STDERR` targets and prevents |
43 | | -> creation of the default output log. To create a log file that persists between |
44 | | -> reboots (in your project's `var/log` directory by default), call the app |
45 | | -> container's [logOutput()][] method. |
| 36 | +- register at least one [Console][] output target while bootstrapping your |
| 37 | + application, or |
| 38 | +- create an [Application][] or [CliApplication][] and optionally use |
| 39 | + [logOutput()][] to write output a persistent log file |
46 | 40 |
|
47 | | -### Output methods |
| 41 | +## Message methods |
48 | 42 |
|
49 | 43 | <!-- prettier-ignore --> |
50 | | -| Method | Message level | Default prefix | Default output target | |
51 | | -| --------------- | --------------------- | ---------------- | ------------------------- | |
52 | | -| `error[Once]()` | `LEVEL_ERROR` = `3` | ` ! ` | `STDERR` | |
53 | | -| `warn[Once]()` | `LEVEL_WARNING` = `4` | ` ^ ` | `STDERR` | |
54 | | -| `info[Once]()` | `LEVEL_NOTICE` = `5` | ` ➤ ` | `STDOUT` | |
55 | | -| `log[Once]()` | `LEVEL_INFO` = `6` | ` - ` | `STDOUT` | |
56 | | -| `debug[Once]()` | `LEVEL_DEBUG` = `7` | ` : ` | `STDOUT` (if `DEBUG` set) | |
57 | | -| `group()`[^2] | `LEVEL_NOTICE` = `5` | ` » ` | `STDOUT` | |
58 | | -| `logProgress()` | `LEVEL_INFO` = `6` | ` ⠋ ` (spinner) | `STDOUT` | |
59 | | - |
60 | | -[^1]: |
61 | | - Actually a facade for [ConsoleInterface][], which is backed by a shared |
62 | | - instance of [Console][ConsoleService] by default. |
63 | | - |
64 | | -[^2]: |
65 | | - `Console::group()` adds a level of indentation to all `Console` output until |
66 | | - `Console::groupEnd()` is called. |
67 | | - |
68 | | -### Formatting |
| 44 | +| Method | Message level | Default prefix | |
| 45 | +| ---------------------------------- | --------------------- | -------------------------- | |
| 46 | +| `error()`, `errorOnce()` | `LEVEL_ERROR` = `3` | ` ! ` | |
| 47 | +| `warn()`, `warnOnce()` | `LEVEL_WARNING` = `4` | ` ^ ` | |
| 48 | +| `group()`,`groupEnd()` | `LEVEL_NOTICE` = `5` | ` » `, ` « ` | |
| 49 | +| `info()`, `infoOnce()` | `LEVEL_NOTICE` = `5` | ` ➤ ` | |
| 50 | +| `log()`, `logOnce()` | `LEVEL_INFO` = `6` | ` - ` | |
| 51 | +| `logProgress()`, `clearProgress()` | `LEVEL_INFO` = `6` | ` ⠋ ` (spinner, TTY only) | |
| 52 | +| `debug()`, `debugOnce()` | `LEVEL_DEBUG` = `7` | ` : ` | |
| 53 | + |
| 54 | +## Formatting |
69 | 55 |
|
70 | 56 | The following Markdown-like syntax is supported in [Console][] messages: |
71 | 57 |
|
@@ -98,13 +84,16 @@ hard line break. |
98 | 84 | https://salient-labs.github.io/toolkit/Salient.Cli.CliApplication.html |
99 | 85 | [Console]: |
100 | 86 | https://salient-labs.github.io/toolkit/Salient.Core.Facade.Console.html |
101 | | -[ConsoleInterface]: |
102 | | - https://salient-labs.github.io/toolkit/Salient.Contract.Console.ConsoleInterface.html |
103 | | -[ConsoleService]: |
104 | | - https://salient-labs.github.io/toolkit/Salient.Console.Console.html |
| 87 | +[error()]: |
| 88 | + https://salient-labs.github.io/toolkit/Salient.Core.Facade.Console.html#_error |
| 89 | +[errorOnce()]: |
| 90 | + https://salient-labs.github.io/toolkit/Salient.Core.Facade.Console.html#_errorOnce |
| 91 | +[log()]: |
| 92 | + https://salient-labs.github.io/toolkit/Salient.Core.Facade.Console.html#_log |
| 93 | +[logger()]: |
| 94 | + https://salient-labs.github.io/toolkit/Salient.Core.Facade.Console.html#_logger |
| 95 | +[logOnce()]: |
| 96 | + https://salient-labs.github.io/toolkit/Salient.Core.Facade.Console.html#_logOnce |
105 | 97 | [logOutput()]: |
106 | 98 | https://salient-labs.github.io/toolkit/Salient.Container.Application.html#_logOutput |
107 | | -[registerStderrTarget()]: |
108 | | - https://salient-labs.github.io/toolkit/Salient.Console.Console.html#_registerStderrTarget |
109 | | -[registerTarget()]: |
110 | | - https://salient-labs.github.io/toolkit/Salient.Console.Console.html#_registerTarget |
| 99 | +[PSR-3]: https://www.php-fig.org/psr/psr-3/ |
0 commit comments