Skip to content
Snippets Groups Projects
Select Git revision
  • e4dbd4ad491f656aaa7f9da9d4dae2c18622a1e6
  • main default protected
  • renovate/main-renovatebot-github-action-43.x
  • next
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • renovate/main-redis-5.x
  • chore/punycode
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • fix/32307-global-extends-merging
  • fix/32307-global-extends-repositories
  • gh-readonly-queue/next/pr-35009-046ebf7cb84ab859f7fefceb5fa53a54ce9736f8
  • 41.45.0
  • 41.44.0
  • 41.43.7
  • 41.43.6
  • 41.43.5
  • 41.43.4
  • 41.43.3
  • 41.43.2
  • 41.43.1
  • 41.43.0
  • 41.42.12
  • 41.42.11
  • 41.42.10
  • 41.42.9
  • 41.42.8
  • 41.42.7
  • 41.42.6
  • 41.42.5
  • 41.42.4
  • 41.42.3
41 results

index.ts

Blame
  • user avatar
    Michael Kriese authored and GitHub committed
    3ab24f92
    History
    index.ts 3.46 KiB
    import is from '@sindresorhus/is';
    import * as bunyan from 'bunyan';
    import { nanoid } from 'nanoid';
    import cmdSerializer from './cmd-serializer';
    import configSerializer from './config-serializer';
    import errSerializer from './err-serializer';
    import { RenovateStream } from './pretty-stdout';
    import type { BunyanRecord, Logger } from './types';
    import { ProblemStream, validateLogLevel, withSanitizer } from './utils';
    
    let logContext: string = process.env.LOG_CONTEXT ?? nanoid();
    let curMeta: Record<string, unknown> = {};
    
    const problems = new ProblemStream();
    
    // istanbul ignore if: not easily testable
    if (is.string(process.env.LOG_LEVEL)) {
      process.env.LOG_LEVEL = process.env.LOG_LEVEL.toLowerCase().trim();
    }
    
    validateLogLevel(process.env.LOG_LEVEL);
    const stdout: bunyan.Stream = {
      name: 'stdout',
      level:
        (process.env.LOG_LEVEL as bunyan.LogLevel) ||
        /* istanbul ignore next: not testable */ 'info',
      stream: process.stdout,
    };
    
    // istanbul ignore else: not testable
    if (process.env.LOG_FORMAT !== 'json') {
      // TODO: typings (#9615)
      const prettyStdOut = new RenovateStream() as any;
      prettyStdOut.pipe(process.stdout);
      stdout.stream = prettyStdOut;
      stdout.type = 'raw';
    }
    
    const bunyanLogger = bunyan.createLogger({
      name: 'renovate',
      serializers: {
        body: configSerializer,
        cmd: cmdSerializer,
        config: configSerializer,
        migratedConfig: configSerializer,
        originalConfig: configSerializer,
        presetConfig: configSerializer,
        oldConfig: configSerializer,
        newConfig: configSerializer,
        err: errSerializer,
      },
      streams: [
        stdout,
        {
          name: 'problems',
          level: 'warn' as bunyan.LogLevel,
          stream: problems as any,
          type: 'raw',
        },
      ].map(withSanitizer),
    });
    
    const logFactory =
      (level: bunyan.LogLevelString): any =>
      (p1: any, p2: any): void => {
        if (p2) {
          // meta and msg provided
          bunyanLogger[level]({ logContext, ...curMeta, ...p1 }, p2);
        } else if (is.string(p1)) {
          // only message provided
          bunyanLogger[level]({ logContext, ...curMeta }, p1);
        } else {
          // only meta provided
          bunyanLogger[level]({ logContext, ...curMeta, ...p1 });
        }
      };
    
    const loggerLevels: bunyan.LogLevelString[] = [
      'trace',
      'debug',
      'info',
      'warn',
      'error',
      'fatal',
    ];
    
    export const logger: Logger = {} as any;
    
    loggerLevels.forEach((loggerLevel) => {
      logger[loggerLevel] = logFactory(loggerLevel);
    });
    
    export function setContext(value: string): void {
      logContext = value;
    }
    
    export function getContext(): any {
      return logContext;
    }
    
    // setMeta overrides existing meta, may remove fields if no longer existing
    export function setMeta(obj: Record<string, unknown>): void {
      curMeta = { ...obj };
    }
    
    // addMeta overrides or adds fields but does not remove any
    export function addMeta(obj: Record<string, unknown>): void {
      curMeta = { ...curMeta, ...obj };
    }
    
    // removeMeta removes the provided fields from meta
    export function removeMeta(fields: string[]): void {
      Object.keys(curMeta).forEach((key) => {
        if (fields.includes(key)) {
          delete curMeta[key];
        }
      });
    }
    
    export /* istanbul ignore next */ function addStream(
      stream: bunyan.Stream
    ): void {
      bunyanLogger.addStream(withSanitizer(stream));
    }
    
    export function levels(name: string, level: bunyan.LogLevel): void {
      bunyanLogger.levels(name, level);
    }
    
    export function getProblems(): BunyanRecord[] {
      return problems.getProblems();
    }
    
    export function clearProblems(): void {
      return problems.clearProblems();
    }