log.ts 894 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { config } from '../config/config'
  2. export class log {
  3. public static Debug(msg:any,...other){
  4. if(config.LOG_OPEN){
  5. console.log(msg,other.length>0?other[0]:"",log.stack())
  6. }
  7. }
  8. private static stack(){
  9. let callingLocation = ''
  10. try {
  11. throw new Error(); // 创建一个新的Error对象,但不实际抛出它
  12. } catch (e) {
  13. const stack = e.stack; // 获取堆栈跟踪信息
  14. // console.log(stack); // 打印堆栈跟踪
  15. // 分析堆栈跟踪,找到log的调用位置
  16. const lines = stack.split('\n');
  17. if(lines.length>=4){
  18. callingLocation = lines[3];
  19. }
  20. }
  21. return callingLocation
  22. }
  23. public static Error(msg:any,...other){
  24. if(config.LOG_OPEN){
  25. console.error(msg,other.length>0?other[0]:"")
  26. }
  27. }
  28. public static Info(msg:any,...other){
  29. if(config.LOG_OPEN){
  30. console.info(msg,other.length>0?other[0]:"")
  31. }
  32. }
  33. }