• 前端打印日志到localStroge并导出


    interface LogEntry {
      data: any
      time: Date
    }
    export class PersistantLog {
      //最大条数
      maxEntries = 3000;
      isEnabled = false;
      name = "default_log";
      localStorage = window.localStorage;
      entries: LogEntry[] = JSON.parse(this.localStorage.getItem(this.name));
      constructor() {
        this.isEnabled = true;
      }
     
      public log(info: any) {
        if (!this.isEnabled) return;
        this.entries.push({
          time: new Date(),
          data: info
        })
        this.save();
      }
     
      public enable() {
        this.isEnabled = true;
      }
     
      private save() {
        this.entries = this.entries.slice(-this.maxEntries)
        let data = JSON.stringify(this.entries)
        this.localStorage.setItem(this.name, data)
      }
     
      public clear() {
        this.localStorage.removeItem(this.name);
      }
      
      private getEntries() {
        return this.entries
      }
     
      public exportLog(exportFileName: string) {
        let resultStr = this.localStorage.getItem(this.name)
        //将文本或者JS字符串信息借助Blob转换成二进制,然后,
        //作为<a>元素的href属性,配合download属性,实现下载
        if ("download" in document.createElement("a")) {
          let eleLink = document.createElement("a")
          eleLink.download = exportFileName + ".json"
          eleLink.style.display = "none"
          let blob = new Blob([resultStr])
          eleLink.href = URL.createObjectURL(blob)

          //兼容firefox,元素添加到页面才能触发点击
          document.body.appendChild(eleLink)
          eleLink.click()
          document.body.removeChild(eleLink)
        }
      }
    }
  • 相关阅读:
    新学期的合作
    软件工程问题及回答
    《程序猿的生命周期》阅读有感
    《构建之法》13~17章
    阅读《构建之法》十一、十二、十三章之感
    阅读《构建之法》十一、十二、十三章
    【.NET / C#】SubarrayUtils(查找子数组工具类)
    【Java】ComplexTimerTask (TimerTask 拓展封装)
    【Java】AesCbcCodec(AES_CBC加解密工具类)
    【Java】AesEcbCodec(AES_ECB加解密工具类)
  • 原文地址:https://www.cnblogs.com/brainworld/p/8662288.html
Copyright © 2020-2023  润新知