Winston: コンソールログは色付けされていません。

作成日 2017年11月10日  ·  22コメント  ·  ソース: winstonjs/winston

コンソール出力を色付けしようとしていますが、機能しません。 出力はすべて同じ(白)色です。 私は3.0.0-rc1を使用しています。

const winston = require('winston');
const logLevels = {
  levels: {
    error: 0,
    warn: 1,
    info: 2,
    http: 3,
    sql: 4,
    debug: 5
  },
  colors: {
    error: "red",
    warn: "darkred",
    info: "black",
    http: "green",
    sql: "blue",
    debug: "gray"
  }
};
winston.addColors(logLevels);
const logger = winston.createLogger({...});
logger.add(new winston.transports.Console({colorize: true}));

私もここで提案を試しましたが、それも機能していません:

logger.add(new winston.transports.Console({format: winston.format.combine(formatter, winston.format.colorize())}));

ありがとう、
アルバロ

最も参考になるコメント

logformのドキュメントを使用して、部分的に機能させることができました。

  const alignedWithColorsAndTime = winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.align(),
    winston.format.printf(info => `${info.timestamp} [${info.level}]: ${info.message}`),
  );

JSON引数のダンプを処理する方法がわかりません。

ここでの更新は私が解決したものです:

  const alignedWithColorsAndTime = winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.align(),
    winston.format.printf((info) => {
      const {
        timestamp, level, message, ...args
      } = info;

      const ts = timestamp.slice(0, 19).replace('T', ' ');
      return `${ts} [${level}]: ${message} ${Object.keys(args).length ? JSON.stringify(args, null, 2) : ''}`;
    }),
  );

screen shot 2017-11-13 at 10 55 05 am

全てのコメント22件

logformのドキュメントを使用して、部分的に機能させることができました。

  const alignedWithColorsAndTime = winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.align(),
    winston.format.printf(info => `${info.timestamp} [${info.level}]: ${info.message}`),
  );

JSON引数のダンプを処理する方法がわかりません。

ここでの更新は私が解決したものです:

  const alignedWithColorsAndTime = winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.align(),
    winston.format.printf((info) => {
      const {
        timestamp, level, message, ...args
      } = info;

      const ts = timestamp.slice(0, 19).replace('T', ' ');
      return `${ts} [${level}]: ${message} ${Object.keys(args).length ? JSON.stringify(args, null, 2) : ''}`;
    }),
  );

screen shot 2017-11-13 at 10 55 05 am

2時間後、 @ Xeoncrossに感謝し、色を機能させることができました。

ウィンストンを開始し、色のないコンソールも手に入れました...
私は次のトランスポート定義コードを使用していました:

new winston.transports.Console({
  format: winston.format.simple()
})

このスレッドを読んだ後、私はこのようにしました:

new winston.transports.Console({
  format: winston.format.combine(
            winston.format.simple(),
            winston.format.colorize()
          )
})

違いはありませんでした。
そこで、colorize形式の位置を1番目に変更して、色を機能させました

new winston.transports.Console({
  format: winston.format.combine(
            winston.format.colorize(),
            winston.format.simple()
          )
})

ありがとう@abrakadobr ! それはトリックをしました。

ここでは何も機能しませんでしたが、まだ色がなく、colorizeを一番上にシフトすると、情報フィールドにエンコードされた文字列が記録されました

シンプルなexpressjsアプリでwinston&morganロギングを使用する方法を示す要点を作成しました。

ありがとう@Xeoncross! 私のフォーマットにwinston.format.colorize()を追加するとうまくいきました。 これがドキュメントにないのは興味深いことです。 または、少なくともドキュメントでそれを見つけることができませんでした。

私は新しい柔軟性が大好きですが、より良いドキュメントが得られることを願っています。 これを機能させるのに時間がかかりすぎました。 winston.cli()を使用していた私たちのプラグアンドプレイの例は、@ Xeoncrossの例と同様に、大いに役立ちます。

現在のリリースバージョンでそれを行う方法を知っている人はいますか? リリース候補バージョンはオプションではありません。

コードを読む際に、ログ全体を色付けするオプションとして、 allcolorizeに送信することもできます。

colorize({ all: true })

@markhealey私はすでにpackage.jsonからwinstonの依存関係を削除してから、あなたのメッセージを読みました。 ありがとう:+1:

Thx @ Xeoncross。 https://github.com/winstonjs/logform/blob/master/timestamp.jsをご覧ください。 format optparamを使用してタイムスタンプをフォーマットできます。 ただし、タイムゾーンには注意してください

format.combine(
    format.colorize(),
    format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    format.align(),
    format.printf(info => {
        const { timestamp, level, message, ...extra } = info;

        return `${timestamp} [${level}]: ${message} ${
            Object.keys(extra).length ? JSON.stringify(extra, null, 2) : ''
        }`;
    }),
)

タイムスタンプが含まれている場合、 format.colorize({all:true}は実際には機能しません。これは、行全体を色付けするための実用的な例です。

const colorizer = winston.format.colorize();

const logger = winston.createLogger({
  level: 'debug',
  format: combine(
    winston.format.timestamp(),
    winston.format.simple(),
    winston.format.printf(msg => 
      colorizer.colorize(msg.level, `${msg.timestamp} - ${msg.level}: ${msg.message}`)
    )
  ),
  transports: [
    new transports.Console(),
  ]

});

フル機能のロガーを確認してください

@abrakadobrに感謝します、あなたの解決策は私のために働きました。

@tommuhm今欠陥があります。 printf全体を実際に色付けしたい場合は、 colorizeに組み込みのフォーマットオプションはありません。 これはあなたにとって意味がありますか?

const { createLogger, format, transports } = require('../');

const logger = createLogger({
  level: 'debug',
  format: format.combine(
    format.timestamp(),
    format.simple(),
    format.printf(info => `${info.timestamp} - ${info.level}: ${info.message}`),
    format.colorize({ all: true })
  ),
  transports: [
    new transports.Console(),
  ]
});

logger.error('wowza');

トリッキーな点は、 colorizer infoが「シリアル化」されたとき(つまり、 info[MESSAGE]が文字列に設定されたとき)に$#$ 3 $#$を認識させることです。 間もなくlogformへのフォローアップPR –掘り下げてくれてありがとう!

@Xeoncross私たちは、はるかに簡単な方法で同じことを達成することもできます

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, colorize, printf } = format;

const level = process.env.LOG_LEVEL || 'debug';

const myFormat = printf(({ level, message, label, timestamp }) => {
    return `${timestamp} ${level}: ${message}`;
});

const logger = createLogger({
    format: combine(colorize(), timestamp(), myFormat),
    transports: [new transports.Console()]
});

参照: https ://www.npmjs.com/package/winston#using -custom-logging-levels

Cloudwatchにログインして色を追加しようとしていますが、色の記号の代わりにwinstonを使用しています。上記のすべてのソリューションを試します。 以下は私のコードです

const winston = require('winston');
const {transports, format, createLogger  } = winston;
const { combine,errors,timestamp} = format;

const colorizer = winston.format.colorize();


const logger = createLogger({
    level:  process.env.LOG_LEVEL,
    prettyPrint : true,
    format: combine(
        winston.format.timestamp(),
        winston.format.simple(),
        winston.format.printf(msg =>
            colorizer.colorize(msg.level, `${msg.timestamp} - ${msg.level}: ${msg.message}`)
        )
    ),
    transports: [
        new transports.Console()
    ]
});

module.exports = {logger};

Cloudwatchコンソールの出力は

[34m2019-08-22T13:00:03.325Z - debug: res by id: [
{
    "isActive": true,
    "value": "fNUMheWiwXayKsaYCbUeA7Gg7BtEPIUbakB56XH1",
    "id": "oehlwqlcve",
    "userId": "6b347b41-ddef-4842-83a0-1cd5ca358482"
}
][39m

この[色の代わりに39mの外観。 なにか提案を?

-22T13:00:03.325Z-デバッグ:IDによる解像度:[

私も同じ問題を抱えています。 なんとか修正できましたか?

-22T13:00:03.325Z-デバッグ:IDによる解像度:[

私も同じ問題を抱えています。 なんとか修正できましたか?

それに関する更新はありますか? なんとか解決しましたか?

@Xeoncross私たちは、はるかに簡単な方法で同じことを達成することもできます

const { createLogger, format, transports } = require('winston');
const { combine, timestamp, colorize, printf } = format;

const level = process.env.LOG_LEVEL || 'debug';

const myFormat = printf(({ level, message, label, timestamp }) => {
  return `${timestamp} ${level}: ${message}`;
});

const logger = createLogger({
  format: combine(colorize(), timestamp(), myFormat),
  transports: [new transports.Console()]
});

参照: https ://www.npmjs.com/package/winston#using -custom-logging-levels

それはMacOSのiTermで私のためにトリックをしました

以下のリンクは私のために働いています:
https://stackoverflow.com/questions/51012150/winston-3-0-colorize-whole-output-on-console

このページは役に立ちましたか?
0 / 5 - 0 評価