1.5.1 • Published 2 years ago

myfavouritelogger v1.5.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

myfavouritelogger

Build Status codecov npm version

This is my favourite logger and it will be yours.

Try it!

Try on Runkit

Why use it?

  1. You want to write cool console logs (colors, dates...).
  2. You want to write file logs with daily rotation and size control.
  3. You know how to use Winston logger but you don't like how it looks out of the box.
  4. You like colors.
  5. You like customization.
  6. You like log levels.
  7. You like custom print formats.
  8. You don't want to programm all this.

How to use it?

// typescript example
import myfavouritelogger from 'myfavouritelogger';

const logger = myfavouritelogger();

logger.error('Hello world!');
logger.warn('Hello world!');
logger.info('Hello world!');
logger.debug('Hello world!'); // not shown, default level is info
// javascript example
const myfavouritelogger = require('myfavouritelogger').default;

const logger = myfavouritelogger();

logger.error('Hello world!');
logger.warn('Hello world!');
logger.info('Hello world!');
logger.debug('Hello world!'); // not shown, default level is info

How to customize it?

import myfavouritelogger from 'myfavouritelogger'; // or require in vanilla javascript

const logger = myfavouritelogger({
    printFormat: (str: string, info: TransformableInfo) => {
        if (info.level) {
            str += mapLevelColor(info.level, `[${info.level}] `);
        }

        if (info.name) {
            str += `[${info.name}] `.green;
        }

        str += `${info.message}`;

        return str;
    },
    file: {
        path: './logs/application-%DATE%.access.log',
        maxSize: '20m',
        maxFiles: '14d',
        zippedArchive: true,
        pathDatePattern: 'YYYY-MM-DD',
    },
    console: true,
    level: 'debug';
    colors: true;
    dateFormat: 'YYYY-MM-DD HH:mm:ssZ';
});

logger.error('Hello world!');
logger.warn('Hello world!');
logger.info('Hello world!');
logger.debug('Hello world!');

Options

OptionTypeDefaultDescription
printFormat(str: string, info: TransformableInfo) => string(see below table)Function to override the print format. str is the original string to print. info is the winston object with context info
file.pathstringNo defaultPath where the log file will be written. The file name can contains %DATE% keyword for daily rotation. If this option is not present, no file will be generated
file.maxSizestring|number'20m'Indicates the max size for the log file. If it reached the maximum, the file will rotate
file.maxFilesstring|number'14d'Indicates the maximum number of files to store. If the files rotates to reach this number, the older files will be deleted
file.zippedArchivebooleanfalseIndicates if the rotated files will be zipped or not
file.pathDatePatternstring'YYYY-MM-DD'If you have set a file path, this option will define the format date for the file name (check momentjs formats for complete documentation)
consolebooleantrueIndicates if the logs will be printed n the console
level'info'|'warning'|'error'|'debug'infoThe log level
colorsbooleantrueIf true, the logs will show beautiful colors
dateFormatstring'YYYY-MM-DD HH:mm:ssZ'Each log line will display the date. This option defines de date format (check momentjs formats for complete documentation)

Default printFormat

function defaultPrint(this: any, str: string, info: TransformableInfo): string {
    if (info.level) {
        str += mapLevelColor(info.level, `[${info.level}] `);
    }
    if (info.name) {
        str += `[${info.name}] `.green;
    }

    str += `${info.message}`;

    return str;
}
1.5.1

2 years ago

1.5.0

2 years ago

1.4.1

2 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.16

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago