Skip to content

Commit

Permalink
chore(docs): Update w/ MySQL transport (#2456)
Browse files Browse the repository at this point in the history
* chore(docs): Update w/ MySQL transport

* Update transports.md

Clean up some of the verbiage and remove some redundancy

---------

Co-authored-by: David Hyde <DABH@users.noreply.github.com>
  • Loading branch information
paulopatto and DABH authored May 9, 2024
1 parent 1d5d527 commit d567c57
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions docs/transports.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ there are additional transports written by
* [Logsene](#logsene-transport) (including Log-Alerts and Anomaly Detection)
* [Logz.io](#logzio-transport)
* [Mail](#mail-transport)
* [MySQL](#mysql-transport)
* [New Relic](#new-relic-agent-transport)
* [Papertrail](#papertrail-transport)
* [PostgresQL](#postgresql-transport)
Expand Down Expand Up @@ -641,6 +642,55 @@ The Mail transport uses [node-mail][17] behind the scenes. Options are the foll

*Metadata:* Stringified as JSON in email.

### MySQL Transport

[winston-mysql](https://github.com/charles-zh/winston-mysql) is a MySQL transport for Winston.

Create a table in your database first:

```sql
CREATE TABLE `sys_logs_default` (
`id` INT NOT NULL AUTO_INCREMENT,
`level` VARCHAR(16) NOT NULL,
`message` VARCHAR(2048) NOT NULL,
`meta` VARCHAR(2048) NOT NULL,
`timestamp` DATETIME NOT NULL,
PRIMARY KEY (`id`));
```

> You can also specify `meta` to be a `JSON` field on MySQL 5.7+, i.e., ``meta` JSON NOT NULL`, which is helfpul for searching and parsing.
Configure Winston with the transport:

```javascript
import MySQLTransport from 'winston-mysql';

const options = {
host: '${MYSQL_HOST}',
user: '${MYSQL_USER}',
password: '${MYSQL_PASSWORD}',
database: '${MYSQL_DATABASE}',
table: 'sys_logs_default'
};

const logger = winston.createLogger({
level: 'debug',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new winston.transports.Console({
format: winston.format.simple(),
}),
new MySQLTransport(options),
],
});

/// ...
let msg = 'My Log';
logger.info(msg, {message: msg, type: 'demo'});
```


### New Relic Agent Transport

[winston-newrelic-agent-transport][47] is a New Relic transport that leverages the New Relic agent:
Expand Down

0 comments on commit d567c57

Please sign in to comment.