|
2 | 2 | title: Logging |
3 | 3 | --- |
4 | 4 |
|
5 | | -Powerful logging capabilities for debugging and tracing debugger activity and connection issues. |
| 5 | +Two different things get called logging here, and they solve different problems. |
| 6 | + |
| 7 | +One is your own output — the `echo` and `var_dump()` you scatter about — arriving |
| 8 | +in your editor instead of the response body. The other is the debugger's own log, |
| 9 | +which is what you reach for when the debugger itself is not behaving. |
| 10 | + |
| 11 | +## Your script's output in the editor |
| 12 | + |
| 13 | +Your editor can ask for a copy of everything the script writes to standard output. |
| 14 | +`echo`, `print_r()`, `var_dump()`, whatever a framework writes — all of it appears |
| 15 | +in a console panel as the request runs, rather than only in the response. |
| 16 | + |
| 17 | +You turn this on in your editor, not in `php.ini`. It is negotiated when the |
| 18 | +session starts, so look for a "capture output" or "console" setting there. |
| 19 | + |
| 20 | +The useful part is *when* it shows up. Output arrives as it is produced, so on a |
| 21 | +request that dies before it can respond, or one whose output you never see because |
| 22 | +it is a background job or an API call made by something else, the console is often |
| 23 | +the only place that output exists. |
| 24 | + |
| 25 | +Editors ask for one of two behaviours: |
| 26 | + |
| 27 | +| Mode | Effect | |
| 28 | +| --- | --- | |
| 29 | +| Copy | The output goes to the editor *and* to the response as normal. | |
| 30 | +| Redirect | The output goes to the editor only, and is stripped from the response. | |
| 31 | + |
| 32 | +Copy is the safe default and what most editors ask for. Redirect is occasionally |
| 33 | +useful when debug output would corrupt a response that has to stay well-formed — |
| 34 | +JSON an API client will parse, say — but remember that anything the client was |
| 35 | +supposed to receive is gone too. |
| 36 | + |
| 37 | +:::note[Standard error is not included] |
| 38 | + |
| 39 | +Only standard output is captured. Capturing standard error is not implemented — an |
| 40 | +editor that asks for it is simply told the request failed — so anything written |
| 41 | +there, such as `error_log()` without a destination, `fwrite()` to `STDERR` or PHP's |
| 42 | +own startup errors, is not in the console. |
| 43 | + |
| 44 | +If something is missing that you are certain was printed, this is usually why. Look |
| 45 | +in the PHP error log for it. |
| 46 | + |
| 47 | +::: |
| 48 | + |
| 49 | +## The debugger's own log |
| 50 | + |
| 51 | +The debug log is for the times the debugger is the problem: the session never |
| 52 | +starts, a breakpoint never fires, the connection dies mid-request. It records what |
| 53 | +the debugger decided and why. |
| 54 | + |
| 55 | +Point it at a file: |
| 56 | + |
| 57 | +```ini |
| 58 | +php_debugger.log=/tmp/php-debugger.log |
| 59 | +``` |
| 60 | + |
| 61 | +The file is appended to, never truncated, and every line is flushed as it is |
| 62 | +written — so a crash does not cost you the last thing it was doing. It is one file |
| 63 | +for every process using it, and lines from concurrent requests interleave, which is |
| 64 | +why each line begins with its PID. |
| 65 | + |
| 66 | +A line looks like this: |
| 67 | + |
| 68 | +``` |
| 69 | +[12345] [Step Debug] INFO: Connecting to configured address/port: localhost:9003. |
| 70 | +``` |
| 71 | + |
| 72 | +That is the PID, the area of the debugger the message came from, the severity, and |
| 73 | +the message itself. When you are staring at a log holding several requests at once, |
| 74 | +the PID is what lets you follow one of them through. |
| 75 | + |
| 76 | +### How much it records |
| 77 | + |
| 78 | +`php_debugger.log_level` is a threshold — everything at or below the number you set |
| 79 | +is written: |
| 80 | + |
| 81 | +| Level | Name | What it adds | |
| 82 | +| --- | --- | --- | |
| 83 | +| 0 | Critical | Failures that stopped the debugger from working at all, such as an invalid mode. | |
| 84 | +| 1 | Error | Things that failed, such as a log file that could not be opened. | |
| 85 | +| 3 | Warning | Recoverable problems, such as falling back after client discovery found nothing. | |
| 86 | +| 5 | Communication | Every protocol message in both directions, as raw XML. | |
| 87 | +| 7 | Info | Connection attempts, sessions starting and ending, breakpoints being resolved. **The default.** | |
| 88 | +| 10 | Debug | Everything, including each trigger check and path-mapping decision. | |
| 89 | + |
| 90 | +The default of `7` is the right place to start, and it answers most questions on |
| 91 | +its own — it tells you whether a connection was attempted, where to, and whether it |
| 92 | +succeeded. |
| 93 | + |
| 94 | +Reach for `10` when the question is "why did the debugger not even try", since that |
| 95 | +is the level that shows the decisions leading up to a connection. Level `5` is a |
| 96 | +different tool: it is for when the debugger and your editor are talking but |
| 97 | +disagreeing, and you need to see what was actually sent. |
| 98 | + |
| 99 | +### Without a log file |
| 100 | + |
| 101 | +Some of it still reaches you. With no log file configured, the debugger writes its |
| 102 | +Error and Critical messages to PHP's own error log instead, so a hard failure |
| 103 | +leaves a trace whether or not you set anything up. |
| 104 | + |
| 105 | +Everything gentler than that is dropped, which is why a session that quietly fails |
| 106 | +to start gives you nothing until you point `php_debugger.log` at a file. |
| 107 | + |
| 108 | +:::tip[Turn it off when you are done] |
| 109 | + |
| 110 | +Every line is flushed to disk as it happens, and at level `10` there are a great |
| 111 | +many lines. That is fine for a debugging session and wasteful for anything else, so |
| 112 | +the log is best switched on for a question and off once you have the answer. |
| 113 | + |
| 114 | +Leaving `php_debugger.log` set on a shared or long-running environment also means a |
| 115 | +file that grows without limit, and one that records paths, trigger values and |
| 116 | +protocol traffic — not something to leave lying around. |
6 | 117 |
|
7 | | -:::note |
8 | | -This page is a work in progress — full documentation is coming soon. |
9 | 118 | ::: |
0 commit comments