From 2756a253156f0076796c68c0b0ef16aae9d706c5 Mon Sep 17 00:00:00 2001 From: Paulo Patto Date: Tue, 30 Apr 2024 15:01:59 -0300 Subject: [PATCH 1/2] chore(docs): Update w/ MySQL transport --- docs/transports.md | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/docs/transports.md b/docs/transports.md index 21c610ae6..a2e75429b 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -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) @@ -641,6 +642,68 @@ 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 MySQL plugin for winston logger. + +#### installation + +Creates a table in the 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`)); +``` + +> Or you can use the JSON format meta field in MySQL database table. That is great for searching & parsing, but it only supports MySQL 5.7+. Bellow the same example but using JSON field. + +```sql + CREATE TABLE `sys_logs_json` ( + `id` INT NOT NULL AUTO_INCREMENT, + `level` VARCHAR(16) NOT NULL, + `message` VARCHAR(2048) NOT NULL, + `meta` JSON NOT NULL, + `timestamp` DATETIME NOT NULL, + PRIMARY KEY (`id`)); + +``` + +Configs the transport to winston: + +```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: From 96c9e2b7221973355532ae572b4a9ee7e3fdf52c Mon Sep 17 00:00:00 2001 From: David Hyde Date: Thu, 9 May 2024 11:20:01 -0500 Subject: [PATCH 2/2] Update transports.md Clean up some of the verbiage and remove some redundancy --- docs/transports.md | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/docs/transports.md b/docs/transports.md index a2e75429b..8bb8cc910 100644 --- a/docs/transports.md +++ b/docs/transports.md @@ -644,11 +644,9 @@ The Mail transport uses [node-mail][17] behind the scenes. Options are the foll ### MySQL Transport -[winston-mysql](https://github.com/charles-zh/winston-mysql) is MySQL plugin for winston logger. +[winston-mysql](https://github.com/charles-zh/winston-mysql) is a MySQL transport for Winston. -#### installation - -Creates a table in the database first: +Create a table in your database first: ```sql CREATE TABLE `sys_logs_default` ( @@ -660,20 +658,9 @@ Creates a table in the database first: PRIMARY KEY (`id`)); ``` -> Or you can use the JSON format meta field in MySQL database table. That is great for searching & parsing, but it only supports MySQL 5.7+. Bellow the same example but using JSON field. - -```sql - CREATE TABLE `sys_logs_json` ( - `id` INT NOT NULL AUTO_INCREMENT, - `level` VARCHAR(16) NOT NULL, - `message` VARCHAR(2048) NOT NULL, - `meta` JSON 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. -Configs the transport to winston: +Configure Winston with the transport: ```javascript import MySQLTransport from 'winston-mysql';