LoginSignup
4
2

More than 5 years have passed since last update.

Angular2(typescript)でめっちゃ簡易な Loggerをつくってみた

Last updated at Posted at 2016-03-02

Angularjs2 開発にてconsole.logを書いては消しがストレスだったので、
もっと気軽にdebugログを出力するため Loggerを作ってみた

後でエラーログをサーバに投げるとかちゃんと実装したい

import {Injectable} from 'angular2/core';
@Injectable()
export class LogService {
  public authenticated: boolean = false;
  //
  // Loglevel
  //  1: error
  //  2: warn
  //  3: notice
  //  4: info
  //  5: debug
  private _logLevel: number = 5;

  public debug(arg: any) {
    if(this._logLevel >= 5) {
      this._log(arg);
    }
  }

  public info(arg: any) {
    if(this._logLevel >= 4) {
      this._log(arg);
    }
  }

  public notice(arg: any) {
    if(this._logLevel >= 3) {
      this._log(arg);
    }
  }

  public warn(arg: any) {
    if(this._logLevel >= 2) {
      this._log(arg);
    }
  }

  public error(arg: any) {
    if(this._logLevel >= 1) {
      this._log(arg);
    }
  }

  private _log(arg: any) {
    console.log(arg);
  }
}

。。。こんだけ

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2