GithubHelp home page GithubHelp logo

angular-ru / angular-logger Goto Github PK

View Code? Open in Web Editor NEW
25.0 3.0 3.0 1.48 MB

Move: https://github.com/Angular-RU/angular-ru-sdk

JavaScript 0.51% TypeScript 91.96% CSS 2.63% HTML 4.90%
angular logger dependency-injection console debugging

angular-logger's Introduction

Angular Logger

Lightweight and configurable Angular logger

Build Status npm version Coverage Status npm-stat

import { LoggerModule } from '@angular-ru/logger';
...

@NgModule({
 imports: [
    LoggerModule.forRoot()
 ],
 ...
})
export class AppModule {}

Motivation

This logger is a handy tool that can be useful in the design and development of the enterprise application level. Easy setting of logging levels and convenient work with groups. Among other things, you can use meta programming (decorators).

Table of contents

Logging

$ npm install @angular-ru/logger --save
import { LoggerModule } from '@angular-ru/logger';
...

@NgModule({
 imports: [
    LoggerModule.forRoot()
 ],
 ...
})
export class AppModule {}

Online examples: https://stackblitz.com/github/Angular-RU/ng-logger

Example: basic methods

import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.trace('trace is worked', 1, { a: 1 });
        this.logger.debug('debug is worked', 2, {});
        this.logger.info('info is worked', 3, Object);
        this.logger.warn('warn is worked', 4, String);
        this.logger.error('error is worked', 5, (2.55).toFixed());
    }
}
  • Default level: All

  • Disable trace on console (filter):
import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    private readonly traceIsWork: string = 'trace is worked';
    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.group('Show trace in opened group', ({ trace }: LoggerService): void => {
            for (let i: number = 0; i < 20; i++) {
                trace(this.traceIsWork, i);
            }
        });
    }
}

Example: groups

  • Logger groups with auto closed (usage callback):
import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    private readonly traceIsWork: string = 'trace is worked';
    private readonly debugIsWork: string = 'debug is worked';
    private readonly infoIsWork: string = 'info is worked';
    private readonly warnIsWork: string = 'warn is worked';
    private readonly errorIsWork: string = 'error is worked';

    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.groupCollapsed('EXAMPLE 2: show stack', () => {
            this.logger.trace(this.traceIsWork, 1, { a: 1 });
            this.logger.debug(this.debugIsWork, 2, console);
            this.logger.info(this.infoIsWork, 3, Object);
            this.logger.warn(this.warnIsWork, 4, String);
            this.logger.error(this.errorIsWork, 5, (2.55).toFixed());
        });

        this.logger.group('Show trace in opened group', ({ trace }: LoggerService): void => {
            for (let i: number = 0; i < 20; i++) {
                trace(this.traceIsWork, i);
            }
        });

        this.logger.groupCollapsed('Show trace in collapsed group', ({ debug }: LoggerService): void => {
            for (let i: number = 0; i < 15; i++) {
                debug(this.traceIsWork, i);
            }
        });
    }
}

Example: nested groups

  • Logger nested groups (with pipe):
import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    private readonly traceIsWork: string = 'trace is worked';
    private readonly debugIsWork: string = 'debug is worked';
    private readonly infoIsWork: string = 'info is worked';
    private readonly warnIsWork: string = 'warn is worked';
    private readonly errorIsWork: string = 'error is worked';

    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger
            .groupCollapsed('GROUP TEST')
            .pipe(({ trace, debug, info, warn, error }: LoggerService) => {
                trace(this.traceIsWork);
                debug(this.debugIsWork);
                info(this.infoIsWork);
                warn(this.warnIsWork);
                error(this.errorIsWork);
            })
            .close();

        this.logger
            .group('A')
            .pipe(
                ({ trace }: LoggerService) => trace(this.traceIsWork),
                ({ debug }: LoggerService) => debug(this.debugIsWork),
                ({ info }: LoggerService) => info(this.infoIsWork),
                ({ warn }: LoggerService) => warn(this.warnIsWork),
                ({ error }: LoggerService) => error(this.errorIsWork)
            )
            .groupCollapsed('B')
            .pipe(
                ({ trace }: LoggerService) => trace(this.traceIsWork),
                ({ debug }: LoggerService) => debug(this.debugIsWork),
                ({ info }: LoggerService) => info(this.infoIsWork),
                ({ warn }: LoggerService) => warn(this.warnIsWork),
                ({ error }: LoggerService) => error(this.errorIsWork)
            )
            .group('C')
            .pipe(
                ({ trace }: LoggerService) => trace(this.traceIsWork),
                ({ debug }: LoggerService) => debug(this.debugIsWork),
                ({ info }: LoggerService) => info(this.infoIsWork),
                ({ warn }: LoggerService) => warn(this.warnIsWork),
                ({ error }: LoggerService) => error(this.errorIsWork)
            )
            .closeAll();
    }
}

Example: set minimal logging level

Basic parameterization

import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    private readonly traceIsWork: string = 'trace is worked';
    private readonly debugIsWork: string = 'debug is worked';
    private readonly infoIsWork: string = 'info is worked';
    private readonly warnIsWork: string = 'warn is worked';
    private readonly errorIsWork: string = 'error is worked';

    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.trace(this.traceIsWork, 1, { a: 1 });
        this.logger.debug(this.debugIsWork, 2, console);
        this.logger.info(this.infoIsWork, 3, Object);
        this.logger.warn(this.warnIsWork, 4, String);
        this.logger.error(this.errorIsWork, 5, (2.55).toFixed());

        this.logger.level = LoggerLevel.INFO;
        this.logger.log('Set new logger level');

        this.logger.trace(this.traceIsWork, 1, { a: 1 });
        this.logger.debug(this.debugIsWork, 2, console);
        this.logger.info(this.infoIsWork, 3, Object);
        this.logger.warn(this.warnIsWork, 4, String);
        this.logger.error(this.errorIsWork, 5, (2.55).toFixed());
    }
}

  • Logger level groups (pretty usage API):
import { LoggerService, LoggerLevel } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    private readonly traceIsWork: string = 'trace is worked';
    private readonly debugIsWork: string = 'debug is worked';
    private readonly infoIsWork: string = 'info is worked';
    private readonly warnIsWork: string = 'warn is worked';
    private readonly errorIsWork: string = 'error is worked';

    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.level = LoggerLevel.INFO;

        this.logger.trace
            .group('A')
            .pipe(
                ({ trace }: LoggerService) => trace(this.traceIsWork),
                ({ debug }: LoggerService) => debug(this.debugIsWork),
                ({ info }: LoggerService) => info(this.infoIsWork),
                ({ warn }: LoggerService) => warn(this.warnIsWork),
                ({ error }: LoggerService) => error(this.errorIsWork)
            )
            .close()

            .debug.group('B')
            .pipe(
                ({ trace }: LoggerService) => trace(this.traceIsWork),
                ({ debug }: LoggerService) => debug(this.debugIsWork),
                ({ info }: LoggerService) => info(this.infoIsWork),
                ({ warn }: LoggerService) => warn(this.warnIsWork),
                ({ error }: LoggerService) => error(this.errorIsWork)
            )
            .close()

            .info.group('C')
            .pipe(
                ({ trace }: LoggerService) => trace(this.traceIsWork),
                ({ debug }: LoggerService) => debug(this.debugIsWork),
                ({ info }: LoggerService) => info(this.infoIsWork),
                ({ warn }: LoggerService) => warn(this.warnIsWork),
                ({ error }: LoggerService) => error(this.errorIsWork)
            )
            .close()

            .warn.group('D')
            .pipe(
                ({ trace }: LoggerService) => trace(this.traceIsWork),
                ({ debug }: LoggerService) => debug(this.debugIsWork),
                ({ info }: LoggerService) => info(this.infoIsWork),
                ({ warn }: LoggerService) => warn(this.warnIsWork),
                ({ error }: LoggerService) => error(this.errorIsWork)
            )
            .close()

            .error.group('E')
            .pipe(
                ({ trace }: LoggerService) => trace(this.traceIsWork),
                ({ debug }: LoggerService) => debug(this.debugIsWork),
                ({ info }: LoggerService) => info(this.infoIsWork),
                ({ warn }: LoggerService) => warn(this.warnIsWork),
                ({ error }: LoggerService) => error(this.errorIsWork)
            )
            .close();

        this.logger.level = LoggerLevel.ALL;
    }
}

Example: set style line

import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.clear();

        this.logger.css('text-transform: uppercase; font-weight: bold').debug('window current ', window);
        this.logger.css('color: red; text-decoration: underline; font-weight: bold').info('It is awesome logger');
        this.logger.debug({ a: 1 });

        this.logger.warn(setStyle);
        this.logger.info('For global configuration, use the constructor parameters');
    }
}

Example: set global style line

import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.clear();

        this.logger.css('font-weight: normal; text-decoration: none; font-style: italic').info(3.14);
        this.logger.css('font-weight: normal;').info(3.14);
        this.logger.warn('global format with style!');
    }
}

Example: CSS classes

import { LoggerModule } from '@angular-ru/logger';

@NgModule({
    // ..
    imports: [
        LoggerModule.forRoot({
            cssClassMap: {
                bold: 'font-weight: bold',
                'line-through': 'text-decoration: line-through',
                'code-sandbox': `
                  color: #666;
                  background: #f4f4f4;
                  border-left: 3px solid #f36d33;
                  font-family: monospace;
                  font-size: 15px;`
            }
        })
    ]
    // ..
})
import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.cssClass('bold line-through').log('JavaScript sucks', 'JavaScript is the best');

        this.logger
            .cssClass('code-sandbox')
            .log('\n   @Component({ .. })' + '\n   export class AppComponent { .. }    \n\n');

        this.logger.cssClass('bold line-through').debug('JavaScript sucks', 'JavaScript is the best');
    }
}
export class AppModule {}

Example: pretty json

import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        // default browser print json
        this.logger.debug('Classic output json', jsonExample);

        // for pretty json usage logger.log method
        this.logger.log(...this.logger.prettyJSON(jsonExample));
    }
}

Example: clipboard

import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        const example: string = 'test string';
        this.logger.log(example);
        this.logger.copy(example);
    }
}

Example: decorators

import { LoggerService, Logger, DebugLog, TraceLog, InfoLog, WarnLog, ErrorLog, Log, LogFn } from '@angular-ru/logger';

export class AppComponent {
    @Logger() public logger: LoggerService;
    @TraceLog() public trace: LogFn;
    @DebugLog() public debug: LogFn;
    @InfoLog() public info: LogFn;
    @ErrorLog() public error: LogFn;
    @WarnLog() public warn: LogFn;
    @Log() public log: LogFn;

    private readonly traceIsWork: string = 'trace is worked';
    private readonly debugIsWork: string = 'debug is worked';
    private readonly infoIsWork: string = 'info is worked';
    private readonly warnIsWork: string = 'warn is worked';
    private readonly errorIsWork: string = 'error is worked';

    public showExample(): void {
        this.logger.clear();
        this.logger.log('log is worked');
        this.trace(this.traceIsWork, 1, { a: 1 });
        this.debug(this.debugIsWork, 2, console);
        this.info(this.infoIsWork, 3, Object);
        this.warn(this.warnIsWork, 4, String);
        this.error(this.errorIsWork, 5, (2.55).toFixed());
    }
}

Example: decorator groups

import { LoggerService, Logger, LoggerLevel, Group } from '@angular-ru/logger';

export class AppComponent {
    @Logger() public logger: LoggerService;

    @Group('test title', LoggerLevel.WARN)
    private helloWorld(name: string): string {
        this.logger.log('log only in group', name);
        return 'hello world';
    }

    public showExample11(): void {
        this.logger.log(this.helloWorld('Hello'));
    }
}

Example: decorator group with function title

import { Log, LogFn, Group } from '@angular-ru/logger';

export class AppComponent {
    @Log() public log: LogFn;

    @Group((name: string) => `Test group with ${name}`)
    public method(name: string): string {
        this.log('group is worked');
        return name;
    }

    public showExample(): void {
        this.method('hello world');
    }
}

Example: timer decorator

import { Log, LogFn, TimerLog, LoggerLevel, LoggerService, Logger } from '@angular-ru/logger';
export class AppComponent {
    @Log() public log: LogFn;
    @Logger() public logger: LoggerService;

    @TimerLog('Test timer')
    public showExample(): void {
        this.logger.clear();
        this.log('test log');
    }

    @TimerLog('Advanced timer', LoggerLevel.WARN, false)
    public showExample(): void {
        this.logger.clear();
        this.log('Advanced test log');
    }
}

Example: format output

import { LoggerModule, NgModule, FormatOutput } from '@angular-ru/logger';

@NgModule({
    //..
    imports: [
        LoggerModule.forRoot({
            format(label: string, labelStyle: string): FormatOutput {
                const date = new Date().toLocaleString('ru-RU').replace(',', '');
                const customLabel: string = `${date} ${label}`;
                return { label: customLabel, style: labelStyle };
            }
        })
    ]
})
export class AppModule {}
import { LoggerService, OnInit } from '@angular-ru/logger';

export class AppComponent implements OnInit {
    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.trace('trace is worked', 1, { a: 1 });
        this.logger.debug('debug is worked', 2, {});
        this.logger.info('info is worked', 3, Object);
        this.logger.warn('warn is worked', 4, String);
        this.logger.error('error is worked', 5, (2.55).toFixed());
    }
}

Example: full configurations

import { LoggerModule, NgModule, LoggerLevel } from '@angular-ru/logger';

@NgModule({
    // ..
    imports: [
        LoggerModule.forRoot({
            useLevelGroup: true,
            globalLineStyle: 'color: red; text-decoration: underline; font-weight: bold; font-size: 15px',
            cssClassMap: {
                bold: 'font-weight: bold',
                'line-through': 'text-decoration: line-through',
                'code-sandbox': `
                  color: #666;
                  background: #f4f4f4;
                  border-left: 3px solid #f36d33;
                  font-family: monospace;
                  font-size: 15px;`
            },
            labelNames: {
                [LoggerLevel.TRACE]: '[trace]',
                [LoggerLevel.DEBUG]: '[debug]',
                [LoggerLevel.INFO]: '[info]',
                [LoggerLevel.WARN]: '[warn]',
                [LoggerLevel.ERROR]: '[error]'
            },
            labelColors: {
                [LoggerLevel.TRACE]: 'violet',
                [LoggerLevel.DEBUG]: 'black',
                [LoggerLevel.INFO]: 'tomato',
                [LoggerLevel.WARN]: 'green',
                [LoggerLevel.ERROR]: 'cyan'
            }
        })
    ]
    // ..
})
export class AppModule {}
import { LoggerService } from '@angular-ru/logger';

export class AppComponent implements OnInit {

  public ngOnInit(): void {
    private readonly traceIsWork: string = 'trace is worked';
    private readonly debugIsWork: string = 'debug is worked';
    private readonly infoIsWork: string = 'info is worked';
    private readonly warnIsWork: string = 'warn is worked';
    private readonly errorIsWork: string = 'error is worked';

    constructor(private readonly logger: LoggerService) {}

    public showExample(): void {
        this.logger.log('Example');
        this.logger.trace(this.traceIsWork, 1, { a: 1 });
        this.logger.debug(this.debugIsWork, 2, console);
        this.logger.info(this.infoIsWork, 3, Object);
        this.logger.warn(this.warnIsWork, 4, String);
        this.logger.error(this.errorIsWork, 5, (2.55).toFixed());
    }
}

Todo

  • Override console
  • Logger method (trace, debug, info, warning, error)
  • Logger group + groupCollapsible (pipes)
  • Logger pretty write object
  • Set style by css
  • Logger level groups (trace, debug, info, warn, error)
  • Clipboard data
  • Set global style
  • Added css classes
  • Dependency Injection for Angular
  • Switch enable/disable default console output
  • Decorators
  • Timers (decorator)
  • Format output console

Authors

Eleonora Zbarskaya, Ivanov Maxim

angular-logger's People

Contributors

dependabot[bot] avatar kingofferelden avatar renovate-bot avatar renovate[bot] avatar splincode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

angular-logger's Issues

add format output

LoggerModule.forRoot({
  format: ({ label, style }) => {
      const date = new Date().toLocaleString('ru-RU').replace(',', '');
      return [ `${date} ${label.toUpperCase()} `, style ];
  }
})

Refactor timer log

console.time("answer time");
alert("Click to continue");
console.timeLog("answer time");

Decorator API

I. Logger decorator

Before

export class AppComponent implements OnInit {
    constructor(private logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.debug('hello world');
    }
}

After

export class AppComponent implements OnInit {
    @Logger() private logger: LoggerService;

    public ngOnInit(): void {
        this.logger.debug('hello world');
    }
}

II. Use only methods

Before

export class AppComponent implements OnInit {
    constructor(private logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.trace('hello world');
        this.logger.debug('hello world');
        this.logger.log('hello world');
        this.logger.warn('hello world');
        this.logger.error('hello world');
    }
}

After

export class AppComponent implements OnInit {
    @Trace() private trace: LogFn;
    @Debug() private debug: LogFn;
    @Log() private log: LogFn;
    @Warn() private warn: LogFn;
    @Error() private error: LogFn;

    public ngOnInit(): void {
        this.trace('hello world');
        this.debug('hello world');
        this.log('hello world');
        this.warn('hello world');
        this.error('hello world');
    }
}

III. Group, GroupCollapsed

Before

export class AppComponent implements OnInit {
    constructor(private readonly logger: LoggerService) {}

    public ngOnInit(): void {
        this.logger.group(
            'Show trace in opened group',
            ({ trace }: LoggerService): void => {
                for (let i: number = 0; i < 20; i++) {
                    trace( 'trace is worked', i);
                }
            }
        );
    }
}

After

export class AppComponent implements OnInit {
    @Trace() private trace: LogFn;
    
    @Group('Show trace in opened group') public ngOnInit(): void {
      for (let i: number = 0; i < 20; i++) {
         this.trace('trace is worked', i);
      }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.