@@ -48,6 +48,7 @@ there are additional transports written by
48
48
* [ Logsene] ( #logsene-transport ) (including Log-Alerts and Anomaly Detection)
49
49
* [ Logz.io] ( #logzio-transport )
50
50
* [ Mail] ( #mail-transport )
51
+ * [ MySQL] ( #mysql-transport )
51
52
* [ New Relic] ( #new-relic-agent-transport )
52
53
* [ Papertrail] ( #papertrail-transport )
53
54
* [ PostgresQL] ( #postgresql-transport )
@@ -641,6 +642,55 @@ The Mail transport uses [node-mail][17] behind the scenes. Options are the foll
641
642
642
643
* Metadata:* Stringified as JSON in email.
643
644
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
+
644
694
### New Relic Agent Transport
645
695
646
696
[ winston-newrelic-agent-transport] [ 47 ] is a New Relic transport that leverages the New Relic agent:
0 commit comments