3.4.0 • Published 28 days ago

@midwayjs/logger v3.4.0

Weekly downloads
896
License
MIT
Repository
github
Last release
28 days ago

@midwayjs/logger

PRs Welcome

@midwayjs/logger is a log module for midway project.

Install

$ npm install @midwayjs/logger --save

Create Logger

import { loggers } from '@midwayjs/logger';

const logger = loggers.createLogger('logger', {
  // some logger options
})

Create With Transport

Create logger with console and file transports instance.

import { loggers, ConsoleTransport, FileTransport } from '@midwayjs/logger';

const logger = loggers.createLogger('logger', {
  transports: {
    console: new ConsoleTransport(),
    file: new FileTransport({
      dir: '...',
      fileLogName: 'app.log',
    }),
  }
})

Create console logger.

const logger = loggers.createLogger('consoleLogger', {
  transports: {
    console: new ConsoleTransport(),
  }
})

Create logger with options mode.

const logger = loggers.createLogger('consoleLogger', {
  transports: {
    console: {
      autoColors: true,
    },
    file: {
      dir: '...',
      fileLogName: 'app.log',
    }
  }
})

Logger Output Method

logger.debug('debug info');
logger.info('启动耗时 %d ms', Date.now() - start);
logger.warn('warning!');
logger.error(new Error('my error'));
logger.write('abcde');

Logger Level

log level is divided into the following categories, and the log level decreases sequentially (the larger the number, the lower the level):

const levels = {
  none: 0,
  error: 1,
  trace: 2,
  warn: 3,
  info: 4,
  verbose: 5,
  debug: 6,
  silly: 7,
  all: 8,
}

Set level for all transports

const logger = loggers.createLogger('logger', {
  // ...
  level: 'warn',
});

// not output
logger.debug('debug info');

// not output
logger.info('debug info');

Format and ContextFormat

Add logger format and context format.

const logger = loggers.createLogger('logger', {
  // ...
  format: info => {
    return `${info.timestamp} ${info.message}`;
  },
  contextFormat: info => {
    return `${info.timestamp} [${info.ctx.traceId}] ${info.message}`;
  }
});

info is a default metadata, include some properties.

Tranports

The actual behavior of the log output we call the transport.The log library has four built-in default Transports.

  • ConsoleTransport Output message to stdout and stderr with color.
  • FileTransport Output message to file and rotate by self.
  • ErrorTransport Inherit FileTransport and only output error message.
  • JSONTransport Inherit FileTransport and output json format.

The above Transports are all registered by default and can be configured by the name when registering.

const logger = loggers.createLogger('consoleLogger', {
  transports: {
    console: {/*...options*/},
    file: {/*...options*/},
    error: {/*...options*/},
    json: {/*...options*/},
  }
});

Implement a new Transport

Inherit Transport abstract class and implement log and close method.

import { Transport, ITransport } from '@midwayjs/logger';

export interface CustomTransportOptions {
  // ...
}

export class CustomTransport extends Transport<CustomTransportOptions> implements ITransport {
  log(level: LoggerLevel | false, meta: LogMeta, ...args) {
    // save file or post to remote server
  }
  
  close() {}
}

Register class to TransportManager before used.

import { TransportManager } from '@midwayjs/logger';

TransportManager.set('custom', CustomTransport);

And you can configure it in your code.

const logger = loggers.createLogger('consoleLogger', {
  transports: {
    custom: {/*...options*/}
  }
});

Default Logger Options

find more options in interface.

License

MIT

3.4.1

28 days ago

3.4.0

1 month ago

3.3.0

2 months ago

3.2.0

4 months ago

3.1.4

5 months ago

3.1.3

5 months ago

3.0.0-beta.20

7 months ago

3.0.0-beta.19

7 months ago

3.0.2

6 months ago

3.0.1

6 months ago

2.19.2

6 months ago

2.19.3

6 months ago

2.19.0

7 months ago

2.19.1

6 months ago

3.0.0

7 months ago

3.0.0-beta.14

7 months ago

3.0.0-beta.15

7 months ago

3.0.0-beta.16

7 months ago

3.0.0-beta.17

7 months ago

3.0.0-beta.18

7 months ago

3.1.2

5 months ago

3.1.1

5 months ago

3.1.0

5 months ago

2.18.0

8 months ago

2.17.0

2 years ago

2.16.3

2 years ago

2.16.1

2 years ago

2.16.2

2 years ago

2.15.0

2 years ago

3.0.0-beta.12

2 years ago

3.0.0-beta.13

2 years ago

2.14.1

2 years ago

2.16.0

2 years ago

2.14.2

2 years ago

2.14.0

2 years ago

3.0.0-beta.10

2 years ago

3.0.0-beta.11

2 years ago

3.0.0-beta.1

2 years ago

3.0.0-beta.3

2 years ago

3.0.0-beta.2

2 years ago

3.0.0-beta.5

2 years ago

3.0.0-beta.4

2 years ago

3.0.0-beta.7

2 years ago

3.0.0-beta.6

2 years ago

3.0.0-beta.9

2 years ago

3.0.0-beta.8

2 years ago

3.0.0-alpha.43

3 years ago

3.0.0-alpha.41

3 years ago

3.0.0-alpha.42

3 years ago

3.0.0-alpha.28

3 years ago

2.13.0

3 years ago

2.11.4-beta.1

3 years ago

2.11.4-beta.q

3 years ago

3.0.0-alpha.6

3 years ago

2.11.3

3 years ago

2.11.2

3 years ago

2.11.1

3 years ago

2.11.0

3 years ago

2.10.18

3 years ago

2.10.15

3 years ago

2.10.15-beta.1

3 years ago

2.10.14

3 years ago

2.10.13

3 years ago

2.10.12

3 years ago

2.10.11

3 years ago

2.10.7

3 years ago

2.10.6

3 years ago

2.10.5

3 years ago

2.10.0-beta.3

3 years ago

2.10.0-beta.4

3 years ago

2.10.0

3 years ago

2.10.0-beta.2

3 years ago

2.8.13

3 years ago

2.8.11

3 years ago

2.8.2

3 years ago

2.8.0

3 years ago

2.7.5

3 years ago

2.7.0

3 years ago

2.6.13

3 years ago

2.6.10

3 years ago

2.6.9

3 years ago

2.6.8

3 years ago

2.6.7

3 years ago

2.6.5

3 years ago

2.6.6

3 years ago

2.6.4

3 years ago

2.6.3

3 years ago

2.6.2

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.0

3 years ago