The Console component provides terminal output and message logging via an API
similar to console
in web browsers.
- Familiar methods like Console::log() and Console::error()
- Variants like Console::logOnce() and Console::errorOnce() to output messages once per run
- Output to multiple targets
- Messages filtered by log level
- Formatting that reflects message priority and improves readability
- Optional Markdown-like syntax for additional formatting
- Colour output to TTYs
- PSR-3 (Logger Interface) support via Console::logger()
If no targets are registered to receive Console output when a message is written:
-
Output is appended to a log file in PHP's default temporary directory, created with mode
0600
if it doesn't already exist:sys_get_temp_dir() . '/<script_basename>-<realpath_hash>-<user_id>.log'
-
If the script is running on the command line, output is also written to
STDERR
.
Debug messages are suppressed unless debug mode is enabled in the environment,
e.g. by setting DEBUG=1
.
To override these defaults:
- register at least one Console output target while bootstrapping your application, or
- create an Application or CliApplication and optionally use logOutput() to write output a persistent log file
Method | Message level | Default prefix |
---|---|---|
error() , errorOnce() |
LEVEL_ERROR = 3 |
! |
warn() , warnOnce() |
LEVEL_WARNING = 4 |
^ |
group() ,groupEnd() |
LEVEL_NOTICE = 5 |
» , « |
info() , infoOnce() |
LEVEL_NOTICE = 5 |
> |
log() , logOnce() |
LEVEL_INFO = 6 |
- |
logProgress() , clearProgress() |
LEVEL_INFO = 6 |
⠋ (spinner, TTY only) |
debug() , debugOnce() |
LEVEL_DEBUG = 7 |
: |
The following Markdown-like syntax is supported in Console messages:
Style | Tag | Typical appearance | Example |
---|---|---|---|
Heading | ___ text ___ *** text *** ## text ## |
Bold + primary colour | ___NAME___ ***NAME*** ## NAME (closing delimiter is optional) |
"Bold" | __ text __ ** text ** |
Bold + default colour | __command__ **command** |
"Italic" | _ text _ * text * |
Secondary colour | _argument_ *argument* |
"Underline" | < text > |
Secondary colour + underline | <argument> |
Low priority | ~~ text ~~ |
Faint | ~~/path/to/script.php:42~~ |
Inline code | ` text ` |
Bold |
The input format can be specified using the `-f/--from` option. |
Code block | ``` text ``` |
|
|
Paragraphs outside code blocks are wrapped to the width reported by the target, and backslash-escaped punctuation characters and line breaks are preserved.
Escaped line breaks may have a leading space, so the following are equivalent:
Text with a \
hard line break.
Text with a\
hard line break.