JavaScript 日志解析器项目:坏主意?
为了我的工作,我每天必须查看大量日志文件。这些日志文件已经存在几个很好的解析器,但我还没有找到我想要的。好吧,谁能比你更适合你,对吧?
我使用 JavaScript 的原因(除了我已经知道它的事实)是因为它是可移植的(不需要安装任何东西),但同时可以跨平台访问。在我投入太多时间之前,这是实现我的目标的糟糕方法吗?
- 输入将被输入到一个文本文件中,由 [x] 分隔,并且值将被放入一个数组中,以便访问这些值比提取静态内容更快。
- 在将值放入数组之前,将处理任何特殊格式(数字、日期等),以防止函数每次使用时重复此步骤。
- 这些日志可能包含 100k+ 行,这对于浏览器来说需要处理很多。然而,每一行并不包含大量信息。
- 我已经写了一些,但即使是 10,000 行,它也开始运行缓慢,我不知道这是因为我效率不够,还是无法有效完成。我想这是因为所有数据都在一张巨大的表中。我可能最好对其进行分页,但这不太理想。
问题 1:有什么我没有提及但应该考虑的事情吗?
问题 2:您能推荐更好的替代方案吗?
问题3:(有点跑题,请忽略)。我不想复制/粘贴输入,而是想“打开”日志文件,但据我所知 JavaScript 无法执行此操作(出于安全原因)。这可以通过 input="file" 来完成,而无需实际上传到服务器吗?我不知道 SSJS 是如何工作的,但看来我低估了 JavaScript 的局限性。
我知道这有点含糊,但我试图让你们不必读一本书来回答我的问题。请告诉我是否应该提供更多详细信息。谢谢!
There are numerous log files that I have to review daily for my job. Several good parsers already exist for these log files but I have yet to find exactly what I want. Well, who could make something more tailored to you than you, right?
The reason I am using JavaScript (other than the fact that I already know it) is because it's portable (no need to install anything) but at the same time cross-platform accessible. Before I invest too much time in this, is this a terrible method of accomplishing my goal?
- The input will be entered into a text file, delimited by [x] and the values will be put into an array to make accessing these values faster than pulling the static content.
- Any special formatting (numbers, dates, etc) will be dealt with before putting the value in the array to prevent a function from repeating this step every time it is used.
- These logs may contain 100k+ lines which will be a lot for the browser to handle. However, each line doesn't contain a ton of information.
- I have written some of it already, but with even 10,000 lines it's starting to run slow and I don't know if it's because I wasn't efficient enough or if this just cannot be effectively done. I'm thinking this is because all the data is in one giant table. I'd probably be better off paginating it, but that is less than desirable.
Question 1: Is there anything I failed to mention that I should consider?
Question 2: Would you recommend a better alternative?
Question 3: (A bit off topic, so feel free to ignore). Instead of copy/pasting the input, I would like to 'open' the log file but as far as I know JavaScript cannot do this (for security reasons). Can this be accomplished with a input="file" without actually having a server to upload to? I don't know how SSJS works, but it appears that I underestimated the limitations of JavaScript.
I understand this is a bit vague, but I'm trying to keep you all from having to read a book to answer my question. Let me know if I should include additional details. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 JavaScript 是一个“不错”的选择。使用脚本语言来解析供个人使用的日志文件是一个非常明智的决定。
但是,我不会为此使用浏览器。 Web 浏览器对一段 javascript 可以运行的时间或允许运行的指令数量或两者都有限制。如果超出这些限制,您将得到如下内容:
因为您将使用大型数据量很大,我怀疑你迟早会遇到这个问题。可以通过巧妙使用 setTimeout 或使用 Web Worker 来避免这种情况,但这会增加项目的复杂性。这可能不是您想要的。
请注意,JavaScript 也可以在浏览器之外运行。例如,Windows 附带 Windows 脚本宿主。这将允许您从命令提示符运行 JavaScript,而无需浏览器。您不会收到“脚本太长”错误。作为额外的奖励,您将拥有完全访问权限到文件系统,以及传递 命令行参数到您的代码。
祝你好运,编码愉快!
I think JavaScript is an "ok" choice for this. Using a scripting language to parse log files for personal use is a perfectly sane decision.
However, I would NOT use a browser for this. Web browsers place limitations on how long a bit of javascript can run, or on how many instructions it is allowed to run, or both. If you exceed these limits, you'll get something like this:
Since you'll be working with a large amount of data, I suspect you're going to hit this sooner or later. This can be avoided by clever use of setTimeout, or potentially with web workers, but that will add complexity to your project. This is probably not what you want.
Be aware that JavaScript can run outside of browsers as well. For instance, Windows comes with the Windows Script Host. This will let you run JavaScript from the command prompt, without needing a browser. You won't get the "Script too long" error. As an added bonus, you will have full access to the file system, and the ability to pass command-line arguments to your code.
Good luck and happy coding!
用粗体回答你的首要问题:不,这不是一个糟糕的想法。
如果 JS 是您所知道的唯一语言,您希望避免设置任何依赖项,并且希望保持平台无关性……JavaScript 似乎非常适合您的特定情况。
作为更一般的规则,我永远不会使用 JS 作为编写桌面应用程序的语言。特别是不适合执行日志解析之类的任务。还有许多其他语言更适合此类问题,例如 Python、Scala、VB 等。我提到 Python 和 Scala 是因为它们的类似脚本的行为和最低的设置要求。 Python 也具有与 JS 非常相似的语法,因此它可能比其他语言更容易学习。如果您拥有 Visual Studio 许可证,VB(或任何 .NET 语言)也可以使用,因为如果它更适合您的需求,它很容易使用 GUI 构建器。
我建议的方法:使用现有的框架。有数百甚至数千个日志解析器可以处理各种用例和不同格式的日志,您应该能够找到接近您需要的内容。可能只需要比谷歌搜索“日志解析器”多一点努力就能找到一个有效的。如果您找不到适合您确切需求的产品,并且愿意花时间自己制作,那么您应该利用这段时间为现有的开源产品之一做出贡献。在尝试第十十亿次重新发明轮子之前,应该始终考虑扩展现有的代码库。
To answer your top question in bold: No, it is not a terrible idea.
If JS is the only language you know, you want to avoid setting up any dependencies, and you want to stay platform-independent... JavaScript seems like a good fit for your particular case.
As a more general rule, I would never use JS as a language to write a desktop app. Especially not for doing a task like log parsing. There are many other languages which are much better suited to this type of problem, like Python, Scala, VB, etc. I mention Python and Scala because of their script-like behaviour and minimal setup requirements. Python also has very similar syntax to JS so it might be easier to pick up then other languages. VB (or any .NET language) would work too if you have a Visual Studio license because of it's easy to use GUI builder if that suits your needs better.
My suggested approach: use an existing framework. There are hundreds, if not thousands of log parsers out there which handle all sorts of use-cases and different formats of logs that you should be able to find something close to what you need. It may just take a little more effort than Google'ing "Log Parsers" to find one that works. If you can't find one that suits your exact needs and you are willing to spend time making your own, you should use that time instead to contribute to one of the existing ones which are open source. Extending an existing code base should always be considered before trying to re-invent the wheel for the 10th gillion time.
考虑到您的不变量“javascript、跨平台、浏览器 ui、尽可能快”,我会考虑这种方法:
考虑到您的评论:
编写应用程序(某种)Javascript(Silverlight)。如果您想坚持使用“经典”Javascript 和浏览器,请编写一个 .HTA 应用程序(对本地计算机的完全访问权限)并使用 ADO 数据(库)访问并尝试获取(旧的)DataGrid/Flexgrid 控件(它们可能已安装;搜索注册表)。
Given your invariants "javascript, cross-platform, browser ui, as fast as possible" I would consider this approach:
Considering your comment:
For Windows-only work you can use the VS Express edition to write an app in C#, VB.NET, C++/CLI, F#, or even (kind of) Javascript (Silverlight). If you want to stick to 'classic' Javascript and a browser, write a .HTA application (full access to the local machine) and use ADO data(base) access and try to get the (old) DataGrid/Flexgrid controls (they may be installed already; search the registry).