Skip to content

Commit d567c57

Browse files
paulopattoDABH
andauthoredMay 9, 2024··
chore(docs): Update w/ MySQL transport (#2456)
* 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>
1 parent 1d5d527 commit d567c57

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
 

Diff for: ‎docs/transports.md

+50
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ there are additional transports written by
4848
* [Logsene](#logsene-transport) (including Log-Alerts and Anomaly Detection)
4949
* [Logz.io](#logzio-transport)
5050
* [Mail](#mail-transport)
51+
* [MySQL](#mysql-transport)
5152
* [New Relic](#new-relic-agent-transport)
5253
* [Papertrail](#papertrail-transport)
5354
* [PostgresQL](#postgresql-transport)
@@ -641,6 +642,55 @@ The Mail transport uses [node-mail][17] behind the scenes. Options are the foll
641642

642643
*Metadata:* Stringified as JSON in email.
643644

645+
### MySQL Transport
646+
647+
[winston-mysql](https://github.com/charles-zh/winston-mysql) is a MySQL transport for Winston.
648+
649+
Create a table in your database first:
650+
651+
```sql
652+
CREATE TABLE `sys_logs_default` (
653+
`id` INT NOT NULL AUTO_INCREMENT,
654+
`level` VARCHAR(16) NOT NULL,
655+
`message` VARCHAR(2048) NOT NULL,
656+
`meta` VARCHAR(2048) NOT NULL,
657+
`timestamp` DATETIME NOT NULL,
658+
PRIMARY KEY (`id`));
659+
```
660+
661+
> 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.
662+
663+
Configure Winston with the transport:
664+
665+
```javascript
666+
import MySQLTransport from 'winston-mysql';
667+
668+
const options = {
669+
host: '${MYSQL_HOST}',
670+
user: '${MYSQL_USER}',
671+
password: '${MYSQL_PASSWORD}',
672+
database: '${MYSQL_DATABASE}',
673+
table: 'sys_logs_default'
674+
};
675+
676+
const logger = winston.createLogger({
677+
level: 'debug',
678+
format: winston.format.json(),
679+
defaultMeta: { service: 'user-service' },
680+
transports: [
681+
new winston.transports.Console({
682+
format: winston.format.simple(),
683+
}),
684+
new MySQLTransport(options),
685+
],
686+
});
687+
688+
/// ...
689+
let msg = 'My Log';
690+
logger.info(msg, {message: msg, type: 'demo'});
691+
```
692+
693+
644694
### New Relic Agent Transport
645695

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

0 commit comments

Comments
 (0)
Please sign in to comment.