|
1 | 1 | # custom-logger |
2 | | -custom-logger is an example of how the Pion API provides an customizable |
3 | | -logging API. By default all Pion projects log to stdout, but we also allow |
4 | | -users to override this and process messages however they want. |
5 | 2 |
|
6 | | -## Instructions |
7 | | -### Download custom-logger |
| 3 | +`custom-logger` is an example demonstrating how to override the default logging behavior of the [Pion WebRTC](https://github.com/pion/webrtc) stack. |
| 4 | +By default, Pion logs everything to `stdout`. |
| 5 | +This example shows how to inject a **custom `LoggerFactory`** to handle logs from every subsystem (ICE, DTLS, SCTP, DataChannel...). |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- Creates a **custom logger** that implements `logging.LeveledLogger`. |
| 12 | +- Initializes two peer connections (`offerer` and `answerer`) locally. |
| 13 | +- Establishes a WebRTC connection between them. |
| 14 | +- Logs events from: |
| 15 | + - `ICE` candidate gathering |
| 16 | + - `DTLS` handshake |
| 17 | + - `SCTP` and `DataChannel` setup |
| 18 | +- Prints logs with clear prefixes like `customLogger Debug:`. |
| 19 | + |
| 20 | +Ideal for: |
| 21 | +- Integrate with external monitoring systems |
| 22 | +- Store logs to files or databases |
| 23 | +- Debug complex WebRTC flows in a structured way |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## How to run |
| 28 | + |
| 29 | +### 1. Install the example |
| 30 | + |
8 | 31 | ``` |
9 | 32 | go install github.com/pion/webrtc/v4/examples/custom-logger@latest |
10 | 33 | ``` |
| 34 | +Make sure ```$(go env GOPATH)/bin ``` is in your ```PATH```. |
11 | 35 |
|
12 | | -### Run custom-logger |
13 | | -`custom-logger` |
| 36 | +You can add it to your PATH like this (zsh): |
| 37 | + |
| 38 | +``` |
| 39 | +echo 'export PATH="$PATH:$(go env GOPATH)/bin"' >> ~/.zshrc |
| 40 | +source ~/.zshrc |
| 41 | +``` |
| 42 | +### 2.Run |
| 43 | +`custom-logger` or `go run main.go` |
| 44 | + |
| 45 | +## Example output |
| 46 | + |
| 47 | +``` |
| 48 | +Creating logger for ice |
| 49 | +Creating logger for dtls |
| 50 | +Peer Connection State has changed: connected (answerer) |
| 51 | +Peer Connection State has changed: connected (offerer) |
| 52 | +customLogger Debug: Adding a new peer-reflexive candidate: 10.8.21.1:51196 |
| 53 | +``` |
14 | 54 |
|
15 | 55 |
|
16 | 56 | You should see messages from our customLogger, as two PeerConnections start a session |
0 commit comments