登录香草JS [英] Logging in vanilla JS

查看:50
本文介绍了登录香草JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何使用Vanilla JS登录浏览器?

How can we do logging in browser using vanilla JS?

我想为我的代码创建日志,该日志可以作为文件存储在客户端(存储在用户的计算机上).如果可以设置最大文件大小,那就太好了.

I want to create logs for my code, which could be stored as a file on client side (stored on user's machine). It would be great if there is an option of setting maximum file size.

如果有人知道解决方案或解决方法,请分享.

If somebody knows a solution or a workaround for this please share.

推荐答案

如果您要从浏览器托管的JavaScript代码中执行此操作,恐怕您不能这样做.基于浏览器的JavaScript代码无法写入用户计算机上的任意文件.这将是一个巨大的安全问题.

If you mean you want to do this from browser-hosted JavaScript code, I'm afraid you can't. Browser-based JavaScript code can't write to arbitrary files on the user's computer. It would be a massive security problem.

您可以保留"log"(日志)在网络存储中,但请注意,网络存储具有大小限制所以你不想让它变得太大.

You could keep a "log" in web storage, but note that web storage has size limits so you wouldn't want to let it grow too huge.

这是添加到本地存储日志的准系统日志功能:

Here's a barebones logging function that adds to a log in local storage:

function log(...msgs) {
    // Get the current log's text, or "" if there isn't any yet
    let text = localStorage.getItem("log") || "";
    // Add this "line" of log data
    text += msgs.join(" ") + "\r\n";
    // Write it back to local storage
    localStorage.setItem("log", text);
}

很显然,您可以通过多种方式(日志级别,日期/时间记录等)在此基础上进行构建.

Obviously you can then build on that in a bunch of different ways (log levels, date/time logging, etc.).

这篇关于登录香草JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆