| 1 | --- |
| 2 | title: Configuring logging |
| 3 | --- |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | pyOCD provides extensive control over log output. It uses the standard Python |
| 8 | [logging](https://docs.python.org/3/library/logging.html) package for all its logging. There are several ways |
| 9 | to set log levels, both globally and with precise control. |
| 10 | |
| 11 | - Verbosity controls |
| 12 | - Logger-level control |
| 13 | - Advanced configuration |
| 14 | |
| 15 | |
| 16 | ## Log levels |
| 17 | |
| 18 | There are multiple log levels, in order from least to most verbose: |
| 19 | - CRITICAL |
| 20 | - ERROR |
| 21 | - WARNING |
| 22 | - INFO |
| 23 | - DEBUG |
| 24 | |
| 25 | The CRITICAL level is used only by the `pyocd` tool for reporting fatal errors. |
| 26 | |
| 27 | Each subcommand for the `pyocd` tool has a default logging level. |
| 28 | |
| 29 | Subcommand | Default level |
| 30 | ---------------|-------------- |
| 31 | `commander` | WARNING |
| 32 | `erase` | WARNING |
| 33 | `flash` | WARNING |
| 34 | `gdbserver` | INFO |
| 35 | `json` | Logging fully disabled |
| 36 | `list` | INFO |
| 37 | `pack` | INFO |
| 38 | `reset` | WARNING |
| 39 | `rtt` | INFO |
| 40 | `server` | INFO |
| 41 | |
| 42 | |
| 43 | ## Basic control |
| 44 | |
| 45 | For most users, the command line `--verbose`/`-v` and `--quiet`/`-q` arguments provide sufficient control |
| 46 | over logging. These arguments can be listed multiple times. Each use increases or decreases the |
| 47 | logging verbosity level. For example, a single `--verbose` moves `pyocd flash` from the default |
| 48 | level of WARNING to INFO. |
| 49 | |
| 50 | |
| 51 | ## Color logging |
| 52 | |
| 53 | By default, log output to a tty is colorised. Control over colorised log output is possible two ways. |
| 54 | |
| 55 | The command-line `--color` argument accepts an optional parameter that must be one of `auto`, `always`, or `never`. |
| 56 | The default is `auto`, which will enable color only when outputting to a tty. |
| 57 | |
| 58 | Another option for controlling color output is the `PYOCD_COLOR` environment variable. It should be set to one of the |
| 59 | same values supported by `--color`. This environment variable changes the default color output setting, and is |
| 60 | overridden by `--color` on the command line. |
| 61 | |
| 62 | |
| 63 | ## Loggers |
| 64 | |
| 65 | Each module in pyOCD uses its own module-specific logger with a name matching the dotted module |
| 66 | name, for instance `pyocd.coresight.fpb`. This lets you control verbosity at the module level. Even |
| 67 | more advanced configurations, such as routing a particular module's log output to a separate file, |
| 68 | are also possible. |
| 69 | |
| 70 | The best way to see which loggers are available is simply to look at the pyOCD source code to see |
| 71 | its package structure. |
| 72 | |
| 73 | |
| 74 | ### Trace loggers |
| 75 | |
| 76 | Certain modules define additional sub-module loggers that output debug trace logs. These loggers always have the |
| 77 | suffix ".trace" and are disabled by default. This ensures the trace messages won't be seen unless explicitly enabled by the `--log-level` / `-L` argument described in the following section. |
| 78 | |
| 79 | Currently defined trace loggers: |
| 80 | |
| 81 | Trace logger | Trace output |
| 82 | --------------------------------------------------------|---------------------------------------------- |
| 83 | `pyocd.coresight.ap.trace` | AP memory transfers |
| 84 | `pyocd.coresight.dap.trace` | AP and DP register accesses |
| 85 | `pyocd.debug.semihost.trace` | Semihost file operations |
| 86 | `pyocd.debug.sequences.scope.trace` | Open-CMSIS-Pack debug sequence variable read/write |
| 87 | `pyocd.debug.sequences.sequences.trace` | Open-CMSIS-Pack debug sequence statements |
| 88 | `pyocd.flash.flash.trace` | Flash algorithm operations |
| 89 | `pyocd.probe.cmsis_dap_probe.trace` | CMSIS-DAP probe API calls |
| 90 | `pyocd.probe.jlink_probe.trace` | Log output from JLink library |
| 91 | `pyocd.probe.pydapaccess.dap_access_cmsis_dap.trace` | CMSIS-DAP packet building |
| 92 | `pyocd.probe.pydapaccess.interface.hidapi_backend.trace` | CMSIS-DAP v1 hidapi backend USB transfers |
| 93 | `pyocd.probe.pydapaccess.interface.pyusb_backend.trace` | CMSIS-DAP v1 pyusb backend USB transfers |
| 94 | `pyocd.probe.pydapaccess.interface.pyusb_v2_backend.trace` | CMSIS-DAP v2 pyusb backend USB transfers |
| 95 | `pyocd.probe.pydapaccess.interface.pywinusb_backend.trace` | CMSIS-DAP v1 pywinusb backend USB transfers |
| 96 | `pyocd.probe.stlink.usb.trace` | STLink USB transfers |
| 97 | `pyocd.probe.tcp_client_probe.trace` | Remote probe client requests and responses |
| 98 | `pyocd.probe.tcp_probe_server.trace` | Remote probe server requests and responses |
| 99 | `pyocd.utility.notification.trace` | Sent notifications |
| 100 | |
| 101 | |
| 102 | ## Logger-level control |
| 103 | |
| 104 | The `--log-level` / `-L` command line argument makes it easy to control logging at the level of individual loggers |
| 105 | or groups of loggers. The argument accepts a comma-separated list of logger names followed by an "=" sign and a |
| 106 | log level name (case-insensitive). |
| 107 | |
| 108 | The logger names are actually glob-style patterns, as supported by many command line shells. This allows use of these |
| 109 | wildcards: |
| 110 | |
| 111 | Pattern | Meaning |
| 112 | ------------|----------------------------------- |
| 113 | `*` | matches everything |
| 114 | `?` | matches any single character |
| 115 | `[seq]` | matches any character in seq |
| 116 | `[!seq]` | matches any character not in seq |
| 117 | `[*]` | match literal "*" |
| 118 | `[?]` | match literal "?" |
| 119 | |
| 120 | The `--log-level` argument can be used more than once on a command line. The arguments are processed in the order they |
| 121 | appear, so later arguments can refine log level settings made by earlier arguments. |
| 122 | |
| 123 | Setting the log level of parent loggers affects the level of all child loggers. |
| 124 | |
| 125 | Examples: |
| 126 | |
| 127 | - `-L 'pyocd.probe.*=debug'`: set all pyOCD debug probe modules to debug log level |
| 128 | - `-L pyocd.core.session,pyocd.core.options=info`: set two modules to info log level |
| 129 | - `-L '*.trace=debug -L *.jlink=warning'`: enable all trace loggers, but set JLink and its trace logger to warnings |
| 130 | only |
| 131 | |
| 132 | Note that you may need to place the value used with `--log-level` in quotes to prevent the shell from attempting to |
| 133 | expand wildcards as file names. |
| 134 | |
| 135 | |
| 136 | ## Advanced control |
| 137 | |
| 138 | Fine-grained control of pyOCD log output is available through logging configuration. The logging |
| 139 | package supports loading a configuration dictionary to control almost all aspects of log output. |
| 140 | |
| 141 | The `logging` session option is used to specify the logging configuration. It can be set to either a |
| 142 | logging configuration dictionary or the path to a YAML file containing a configuration dictionary. |
| 143 | Usually it is easiest to include the configuration directly in a `pyocd.yaml` config file. See the |
| 144 | [configuration documentation]({% link _docs/configuration.md %}) for more on config files. The file path is most |
| 145 | useful when passing the `logging` option via the command line, since you can't provide a dictionary |
| 146 | this way. |
| 147 | |
| 148 | |
| 149 | ### Controlling module log levels |
| 150 | |
| 151 | A basic logging configuration to control verbosity at the module level looks like this, as shown |
| 152 | in a `pyocd.yaml` config file: |
| 153 | |
| 154 | ```yaml |
| 155 | logging: |
| 156 | loggers: |
| 157 | pyocd.flash.loader: |
| 158 | level: DEBUG |
| 159 | pyocd.flash.flash_builder: |
| 160 | level: DEBUG |
| 161 | ``` |
| 162 | |
| 163 | The top level `logging` key is the session option. Under it must be a `loggers` key, which has the |
| 164 | name of each module you wish to configure as a child key. Then, under each module name, the `level` |
| 165 | key specifies the log level for that module. Due to the way logging propagation works, you do not |
| 166 | need to set the level of parent loggers to match the child levels. In fact, setting the level of a |
| 167 | parent logger |
| 168 | such as `pyocd` will set the level for all children—this is an easy way to control the log level |
| 169 | for all of pyOCD. |
| 170 | |
| 171 | Note that because the `logging` option is passed to and handled by the Python logging module, it does not support |
| 172 | wildcard matching against loggers like the `--log-level` argument. |
| 173 | |
| 174 | |
| 175 | ### Full control |
| 176 | |
| 177 | The full schema for the logging configuration dictionary is documented in the |
| 178 | [logging.config module documentation](https://docs.python.org/3/library/logging.config.html#logging-config-dictschema). |
| 179 | The logging module's |
| 180 | [advanced tutorial](https://docs.python.org/3/howto/logging.html#logging-advanced-tutorial) |
| 181 | has a good introduction to the features and log output flow, so you can better understand the |
| 182 | configuration schema. |
| 183 | |
| 184 | The `version` key described in the schema is optional in pyOCD's logging configuration. If not |
| 185 | present, pyOCD will set the schema version to 1 (currently the only version). In addition, pyOCD |
| 186 | will set the `disabled_existing_loggers` key to false unless it is specified in the configuration |
| 187 | (the default is true). |
| 188 | |
| 189 | Note that if you change the configuration for the root logger, you will need to define a handler |
| 190 | and formatter in the configuration (see the example below). |
| 191 | |
| 192 | Here is a much more complex example configuration that sets a custom formatter and changes several log |
| 193 | levels: |
| 194 | |
| 195 | ```yaml |
| 196 | logging: |
| 197 | formatters: |
| 198 | brief: |
| 199 | format: '%(relativeCreated)07d - %(levelname)s - %(name)s - %(message)s' |
| 200 | handlers: |
| 201 | console: |
| 202 | class: logging.StreamHandler |
| 203 | formatter: brief # reference to "brief" formatter above |
| 204 | level: DEBUG |
| 205 | stream: ext://sys.stdout |
| 206 | root: |
| 207 | level: INFO |
| 208 | handlers: |
| 209 | - console # reference to "console" handler above |
| 210 | loggers: |
| 211 | pyocd: |
| 212 | level: INFO # set all pyocd loggers to INFO level |
| 213 | pyocd.probe: |
| 214 | level: DEBUG # set this logger to DEBUG level |
| 215 | ``` |
| 216 | |
| 217 | This example shows how to direct log output to a log file called `pyocd_log.txt`: |
| 218 | |
| 219 | ```yaml |
| 220 | logging: |
| 221 | root: |
| 222 | handlers: [logfile] |
| 223 | formatters: |
| 224 | precise: |
| 225 | format: "[%(relativeCreated)07d:%(levelname)s:%(module)s] %(message)s" |
| 226 | handlers: |
| 227 | logfile: |
| 228 | class: logging.FileHandler |
| 229 | formatter: precise |
| 230 | filename: pyocd_log.txt |
| 231 | mode: w |
| 232 | delay: false |
| 233 | ``` |