|
4 | 4 | ## Introduction
|
5 | 5 | Logging allows you to provide transparency in your application. If you want to learn more about the processing and details of your application parts, logging is a good way. Supercharge provides a solid logger using the [Winston](https://github.com/winstonjs/winston) logging library.
|
6 | 6 |
|
7 |
| -The logging utility coming with Supercharge is configurable to make it seamless for you to log to different destinations, like the console or a log file. |
| 7 | +Supercharge comes with a built-in logger. The logger is configurable making it seamless for you logging to different destinations, like the console or a log file. |
8 | 8 |
|
9 | 9 |
|
10 | 10 | ## Configuraiton
|
11 |
| -Tba. |
| 11 | +All of the logger’s configuration options are inside of the `config/logging.ts` configuration file. The config file allows you to customize the default logging driver and channel-specific configuration. |
| 12 | + |
| 13 | +By default, Supercharg uses the `console` driver printing all log messages to the terminal. When using the `file` driver, you may adjust the log file name. The default log file points to the `storage/logs/app.log` file. |
12 | 14 |
|
13 | 15 |
|
14 | 16 | ### Available Log Drivers
|
15 |
| -Tba. |
| 17 | +Supercharge uses a driver-based approach for logging. A driver represents a single channel or multiple channels. A channel itself describes the destiation for log messages. Here’s a list of available log drivers and the related log channels: |
| 18 | + |
| 19 | +| Channel Driver | Description | |
| 20 | +|----------------- |--------------------------------------------- | |
| 21 | +| `console` | Logging all messages to the terminal | |
| 22 | +| `file` | Logging all messages to a dedicated log file | |
| 23 | + |
| 24 | + |
| 25 | +```info |
| 26 | +At this point, you can’t configure a “stacked” logger driver that sends log messages to multiple destinations. It’s likely this will be added in future releases. |
| 27 | +``` |
16 | 28 |
|
17 | 29 |
|
18 | 30 | ## Using the Logger
|
19 |
| -The logging utility is part of the Supercharge framework. It provides methods for seven different log levels: |
| 31 | +Supercharge bootstraps and configures the logger while starting the application. You can access the logger via the application instance or the log facace: |
20 | 32 |
|
21 | 33 | ```js
|
22 |
| -const Logger = require('@supercharge/framework/logging') |
23 |
| - |
24 |
| -Logger.error(message) |
25 |
| -Logger.warn(message) |
26 |
| -Logger.info(message) |
27 |
| -Logger.verbose(message) |
28 |
| -Logger.debug(message) |
29 |
| -Logger.silly(message) |
| 34 | +import { Logger } from '@supercharge/facades' |
| 35 | + |
| 36 | +Logger.info('Hello Marcus') |
| 37 | + |
| 38 | +// or |
| 39 | + |
| 40 | +import { App } from '@supercharge/facades' |
| 41 | + |
| 42 | +App.logger().info('Hello Marcus') |
| 43 | +``` |
| 44 | + |
| 45 | +**Notice:** you can access the logger wherever you have the `app` instance available. For example, inside of controllers or service providers you may log messages like this: |
| 46 | + |
| 47 | +```ts |
| 48 | +this.app().logger().info('Hello Marcus') |
30 | 49 | ```
|
31 | 50 |
|
32 |
| -The logger provides a method corresponding to the supported log levels. |
| 51 | + |
| 52 | +### Available Logger Methods |
| 53 | +The logger provides the log methods representing individual log levels. The logger follows the syslog protocol from [RFC5424](https://tools.ietf.org/html/rfc5424) defining eight log levels: |
| 54 | + |
| 55 | +```ts |
| 56 | +import { Logger } from '@supercharge/facades' |
| 57 | + |
| 58 | +Logger.emergency(message); |
| 59 | +Logger.alert(message); |
| 60 | +Logger.critical(message); |
| 61 | +Logger.error(message); |
| 62 | +Logger.warning(message); |
| 63 | +Logger.notice(message); |
| 64 | +Logger.info(message); |
| 65 | +Logger.debug(message); |
| 66 | +``` |
33 | 67 |
|
34 | 68 |
|
35 | 69 | ### Logging Context Data
|
36 |
| -Tba. |
| 70 | +You can also pass a context data starting from the second argument to the log methods. Typically you’re passing a context object as the second argument containing all your information: |
| 71 | + |
| 72 | +```js |
| 73 | +import { Logger } from '@supercharge/facades' |
| 74 | + |
| 75 | +Logger.info(`User successfully logged in`, { userId: user.id }) |
| 76 | +``` |
0 commit comments