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)
    }
  }
}

前端打印日志到localStroge并导出的更多相关文章

  1. vue+sentry 前端异常日志监控

    敲代码最糟心不过遇到自己和测试的环境都OK, 客户使用有各种各样还复现不了的问题,被逼无奈只能走到这一步:前端异常日志监控! vue官方文档如下推荐: 就是说, vue有错误机制处理errorHand ...

  2. Xcode8中处理打印日志的配置

    Xcode8中处理打印日志的配置

  3. Mybatis框架基于映射文件和配置文件的方式,实现增删改查,可以打印日志信息

    首先在lib下导入: 与打印日志信息有关的架包 log4j-1.2.16.jar mybatis架包:mybatis-3.1.1.jar 连接数据库的架包:mysql-connector-java-5 ...

  4. Log打印日志遇到的问题

    Log日志打印出现空指针问题 AndroidRuntime(372): Caused by: java.lang.NullPointerException: println needs a messa ...

  5. 重写NSLog,Debug模式下打印日志和当前行数

    在pch文件中加入以下命令,NSLog在真机测试中就不会打印了 //重写NSLog,Debug模式下打印日志和当前行数 #if DEBUG #define NSLog(FORMAT, ...) fpr ...

  6. 基于window.onerror事件 建立前端错误日志

    QA不是万能的,用户的浏览环境非常复杂,很多情况无法靠测试用例去覆盖,所以最好建立一个前端错误日志,在真实用户端收集bug. try&catch是一个捕获前端错误的常见方法,比如: { //给 ...

  7. android112 c代码打印日志,c反编译调用java

    activity: package com.itheima.ccalljava; import android.os.Bundle; import android.app.Activity; impo ...

  8. ruby脚本打印日志到rspec的报告文件中

    在通过ruby+webdriver+rspec做自动化测试的时候,为了便于观察用例执行情况,我基本上都会用 rspec XX.rb --format doc -o result.log 如果遇到失败的 ...

  9. Log4j配置的经典总结,打印日志文件,日志存库

        一.介绍 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制 日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器.NT的事件记录器.UNIX Sy ...

随机推荐

  1. docker——三剑客之Docker Compose

    编排(Orchestration)功能是复杂系统实现灵活可操作性的关键.特别是在Docker应用场景中,编排意味着用户可以灵活的对各种容器资源实现定义和管理. 作为Docker官方编排工具,Compo ...

  2. Linux下安装系统清理软件 BleachBit 1.4

    sudo add-apt-repository ppa:n-muench/programs-ppasudo apt-get updatesudo apt-get install bleachbit

  3. iframe 跨域问题解决方案 利用window.name+iframe跨域获取数据详解

    详解 前文提到用jsonp的方式来跨域获取数据,本文为大家介绍下如何利用window.name+iframe跨域获取数据. 首先我们要简单了解下window.name和iframe的相关知识.ifra ...

  4. 两步实现在Git Bash中用Sublime打开文件

    每次都要用鼠标点来点去才能用sublime打开文件!太不科学!今天来配置一下在Git bash中用sublime打开文件 方法 新建一个文件命名为你想要的命令,比如 subl(注意不能有后缀名),内容 ...

  5. 能否通过六面照片构建3D模型?比如人脸,全身的多角度照片,生成3D模型。?

    https://www.zhihu.com/question/36412840 9023 ​添加评论 ​分享 ​邀请回答​举报 ​ 收起 已关注写回答   9 个回答 默认排序​ 叛逆者 计算机图形学 ...

  6. HDU 2680 Choose the best route(多起点单终点最短路问题)题解

    题意:小A要乘车到s车站,他有w个起始车站可选,问最短时间. 思路:用Floyd超时,Dijkstra遍历,但是也超时.仔细看看你会发现这道题目好像是多源点单终点问题,终点已经确定,那么我们可以直接转 ...

  7. Mac下安装hexo Error: Cannot find module './build/Release/DTraceProviderBindings 解决

    参考: Github:Mac 下已经装了hexo,仍旧报错 官方文档 $ npm install hexo --no-optional if it doesn't work try $ npm uni ...

  8. POJ 2288 Islands and Bridges(状压dp)

    http://poj.org/problem?id=2288 题意: 有n个岛屿,每个岛屿有一个权值V,一条哈密顿路径C1,C2,...Cn的值为3部分之和: 第1部分,将路径中每个岛屿的权值累加起来 ...

  9. hdu 1251 trie树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Problem De ...

  10. postman 安装桌面版

    https://github.com/postmanlabs/postman-app-support