Log.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { Utils } from "./Utils";
  2. /**
  3. * 日志的时间显示设置
  4. */
  5. export enum TimeType {
  6. /**
  7. * 全部显示
  8. */
  9. FULL,
  10. /**
  11. * 不显示时间
  12. */
  13. NoTime,
  14. /**
  15. * 不显示毫秒
  16. */
  17. NoMilliseconds,
  18. /**
  19. * 不显示日期
  20. */
  21. NODate
  22. }
  23. /**
  24. * 日志工具
  25. * @author 袁浩
  26. */
  27. export class Log {
  28. public static greytring = '\x1B[90m' + '%s' + '\x1B[39m';
  29. public static black = '\x1B[30m' + '%s' + '\x1B[39m';
  30. public static blue = '\x1B[34m' + '%s' + '\x1B[39m';
  31. public static cyan = '\x1B[36m' + '%s' + '\x1B[39m';
  32. public static green = '\x1B[32m' + '%s' + '\x1B[39m';
  33. public static magenta = '\x1B[35m' + '%s' + '\x1B[39m';
  34. public static red = '\x1B[31m' + '%s' + '\x1B[39m';
  35. public static yellow = '\x1B[33m' + '%s' + '\x1B[39m';
  36. /**
  37. * 打印日志,控制台颜色是紫色
  38. * @param message 信息
  39. */
  40. public static info(message: string): void {
  41. console.log(this.blue + this.magenta, this.now, " " + message);
  42. }
  43. /**
  44. * 打印错误,控制台颜色是红色
  45. * @param message
  46. */
  47. public static error(message: string, stack: string = null): void {
  48. console.error(this.blue + this.red, this.now, " " + message + (stack ? "\n" + stack : ""));
  49. }
  50. /**
  51. * 警告,控制台颜色是橙色
  52. * @param message
  53. */
  54. public static warn(message: string): void {
  55. console.warn(this.now + " " + message);
  56. }
  57. public static trace(message: string): void {
  58. console.log(this.blue + this.yellow, this.now, " " + message);
  59. }
  60. public static log(message: string): void {
  61. console.log(this.blue + this.green, this.now, " " + message);
  62. }
  63. public static timeType = TimeType.NoMilliseconds;
  64. public static get now(): string {
  65. switch (this.timeType) {
  66. case TimeType.NODate:
  67. return "<" + Utils.formatDate(new Date(), "hh:mm:ss S") + ">";
  68. case TimeType.NoMilliseconds:
  69. return "<" + Utils.formatDate(new Date(), "yyyy-MM-dd hh:mm:ss") + ">";
  70. case TimeType.NoTime:
  71. return "";
  72. default:
  73. return "<" + Utils.formatDate(new Date(), "yyyy-MM-dd hh:mm:ss S") + ">";
  74. }
  75. }
  76. }