Skip to content

Commit 78ef1f2

Browse files
committed
Merge branch 'review-console'
2 parents a802922 + 9d7225e commit 78ef1f2

File tree

17 files changed

+1246
-956
lines changed

17 files changed

+1246
-956
lines changed

docs/Console.md

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,57 @@
1-
## Terminal output and logging
1+
# Console
22

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.
55

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
99
- Output to multiple targets
1010
- 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
1213
- Colour output to TTYs
14+
- [PSR-3 (Logger Interface)][PSR-3] support via [Console::logger()][logger()]
1315

14-
### Default targets
16+
## Default targets
1517

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:
1820

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:
2623

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+
```
2827

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`.
3330

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`.
3633

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:
4035

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
4640

47-
### Output methods
41+
## Message methods
4842

4943
<!-- 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
6955

7056
The following Markdown-like syntax is supported in [Console][] messages:
7157

@@ -98,13 +84,16 @@ hard line break.
9884
https://salient-labs.github.io/toolkit/Salient.Cli.CliApplication.html
9985
[Console]:
10086
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
10597
[logOutput()]:
10698
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

Comments
 (0)