Преглед на файлове

storage-node-v2: Update elastic logger.

- introduce ELASTIC_LOG_LEVEL env-variable.
Shamil Gadelshin преди 3 години
родител
ревизия
2cc56a9fb4
променени са 4 файла, в които са добавени 35 реда и са изтрити 17 реда
  1. 2 0
      colossus.Dockerfile
  2. 2 1
      docker-compose.yml
  3. 3 1
      storage-node-v2/src/commands/server.ts
  4. 28 15
      storage-node-v2/src/services/logger.ts

+ 2 - 0
colossus.Dockerfile

@@ -23,6 +23,8 @@ ENV ACCOUNT_PWD=
 # Optional variables
 ENV SYNC_INTERVAL=1
 ENV ELASTIC_SEARCH_HOST=
+# warn, error, debug, info
+ENV ELASTIC_LOG_LEVEL=debug
 # - overrides account key file
 ENV ACCOUNT_URI=
 

+ 2 - 1
docker-compose.yml

@@ -39,7 +39,8 @@ services:
       - QUERY_NODE_HOST=graphql-server-mnt:${GRAPHQL_SERVER_PORT}
       - WORKER_ID=0
       - ACCOUNT_URI=//Alice
-
+      # enable ElasticSearch server
+      # - ELASTIC_SEARCH_HOST=host.docker.internal:9200
   db:
     image: postgres:12
     restart: always

+ 3 - 1
storage-node-v2/src/commands/server.ts

@@ -64,7 +64,9 @@ export default class Server extends ApiCommandBase {
     elasticSearchHost: flags.string({
       char: 'e',
       required: false,
-      description: 'Elasticsearch host and port (e.g.: some.com:8081).',
+      description: `Elasticsearch host and port (e.g.: some.com:8081).
+Log level could be set using the ELASTIC_LOG_LEVEL enviroment variable.
+Supported values: warn, error, debug, info. Default:debug`,
     }),
     disableUploadAuth: flags.boolean({
       char: 'a',

+ 28 - 15
storage-node-v2/src/services/logger.ts

@@ -3,6 +3,22 @@ import expressWinston from 'express-winston'
 import { Handler, ErrorRequestHandler } from 'express'
 import { ElasticsearchTransport } from 'winston-elasticsearch'
 
+/**
+ * ElasticSearch server date format.
+ */
+const elasticDateFormat = 'YYYY-MM-DDTHH:mm:ss'
+
+/**
+ * Possible log levels.
+ */
+const levels = {
+  error: 0,
+  warn: 1,
+  info: 2,
+  http: 3,
+  debug: 4,
+}
+
 /**
  * Creates basic Winston logger. Console output redirected to the stderr.
  *
@@ -10,15 +26,6 @@ import { ElasticsearchTransport } from 'winston-elasticsearch'
  *
  */
 function createDefaultLoggerOptions(): winston.LoggerOptions {
-  // Levels
-  const levels = {
-    error: 0,
-    warn: 1,
-    info: 2,
-    http: 3,
-    debug: 4,
-  }
-
   const level = () => {
     const env = process.env.NODE_ENV || 'development'
     const isDevelopment = env === 'development'
@@ -97,10 +104,7 @@ export function httpLogger(elasticSearchEndpoint?: string): Handler {
 
   const opts: expressWinston.LoggerOptions = {
     transports,
-    format: winston.format.combine(
-      winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
-      winston.format.json()
-    ),
+    format: winston.format.combine(winston.format.timestamp({ format: elasticDateFormat }), winston.format.json()),
     meta: true,
     msg: 'HTTP {{req.method}} {{req.url}}',
     expressFormat: true,
@@ -158,7 +162,7 @@ function createElasticLogger(elasticSearchEndpoint: string): winston.Logger {
 
   // Formats
   loggerOptions.format = winston.format.combine(
-    winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
+    winston.format.timestamp({ format: elasticDateFormat }),
     winston.format.printf((info) => `${info.timestamp} ${info.level}: ${info.message}`)
   )
 
@@ -200,8 +204,17 @@ export function initElasticLogger(elasticSearchEndpoint: string): void {
  * @returns elastic search winston transport
  */
 function createElasticTransport(elasticSearchEndpoint: string): winston.transport {
+  const possibleLevels = ['warn', 'error', 'debug', 'info']
+
+  let elasticLogLevel = process.env.ELASTIC_LOG_LEVEL ?? ''
+  elasticLogLevel = elasticLogLevel.toLowerCase().trim()
+
+  if (!possibleLevels.includes(elasticLogLevel)) {
+    elasticLogLevel = 'debug' // default
+  }
+
   const esTransportOpts = {
-    level: 'warn',
+    level: elasticLogLevel,
     clientOpts: { node: elasticSearchEndpoint, maxRetries: 5 },
     index: 'storage-node',
   }