简体   繁体   English

在 Python 中读取和打印与 USB 相关的 var 日志消息

[英]Reading and printing var log messages related to USB in Python

I want to print the lines related to USB of the log messages created in Ubuntu.我想打印与 Ubuntu 中创建的日志消息的 USB 相关的行。 Here's my code:这是我的代码:

>>> import re
>>> fd = open("/var/log/syslog.1", "r")
>>> for lines in fd:
...     if re.match("usb", lines):
...             print lines

(By the way, I'm not sure if the file syslog.1 is the right one. However, I do find a lot of messages in it and some related to usb) (顺便说一句,我不确定文件 syslog.1 是否正确。但是,我确实在其中发现了很多消息,其中一些消息与 USB 相关)

@SidNoob: Adding to the topic of logs (may not be python related), I am assuming you are looking for logs from the usb daemon or driver. @SidNoob:添加到日志主题(可能与 python 无关),我假设您正在寻找来自 USB 守护程序或驱动程序的日志。 If thats the case you are probably looking at the right log file "/var/log/syslog" which logs kernel related messages (drivers inclusive).如果是这种情况,您可能正在查看正确的日志文件“/var/log/syslog”,该文件记录了与内核相关的消息(包括驱动程序)。 If it is an application (with a different log level) then you might have to look at "/var/logs/messages".如果它是一个应用程序(具有不同的日志级别),那么您可能需要查看“/var/logs/messages”。

But coming to the POINT: sometimes opening a file like "/var/log/syslog" for reading might be highly memory intensive because you are loading the file into the RAM.但重点是:有时打开像“/var/log/syslog”这样的文件进行读取可能会占用大量内存,因为您正在将文件加载到 RAM 中。 In server type environments this could run into MBs long or even longer.在服务器类型的环境中,这可能会达到 MB 甚至更长。 There is a command "dmesg" which just prints out to the stdout whatever the contents of ""var/log/syslog" file is. So instead of opening this large file you could also store "dmesg"'s output into a string and parse it line by line.无论“var/log/syslog”文件的内容是什么,有一个命令“dmesg”都会打印到标准输出。因此,除了打开这个大文件之外,您还可以将“dmesg”的输出存储到一个字符串中,然后逐行解析。

You might think both ways would be the same and may occupy the same memory in running the python program.您可能认为这两种方式是相同的,并且在运行 python 程序时可能会占用相同的内存。 Yes, but not if you clear out the "dmesg" output.是的,但如果您清除“dmesg”输出,则不会。 "dmesg" is a ring buffer so you can clear it out using "dmesg -c" before running the usb application and issue "dmesg" after you start the application. “dmesg”是一个环形缓冲区,因此您可以在运行 USB 应用程序之前使用“dmesg -c”清除它,并在启动应用程序后发出“dmesg”。 By this way you reduce the amount of lines you parse and hence speeding up your program.通过这种方式,您可以减少解析的行数,从而加快程序速度。

Hope this adds to your knowledge and your program.希望这会增加您的知识和您的程序。

For the most recent messages, you want /var/log/syslog - /var/log/syslog.1 is basically a backup of older messages.对于最近的消息,您需要/var/log/syslog - /var/log/syslog.1基本上是旧消息的备份。

The thing stopping your code from working is that you're using re.match() instead of re.search() - as you can see from the documentation at those links, re.match() only matches at the beginning of the text being searched.阻止您的代码工作的原因是您使用的是re.match()而不是re.search() - 正如您从这些链接的文档中看到的那样, re.match()仅匹配文本的开头被搜查。

In fact, though, you don't need to use regular expressions at all.但实际上,您根本不需要使用正则表达式。 Here's an alternative to your code:这是您的代码的替代方法:

>>> with open("/var/log/syslog") as f:
...     for line in f:
...         if "usb" in line.lower():
...             print line
... 

A few things are different here:这里有一些不同之处:

  1. We're using thewith context manager to open the file.我们使用with上下文管理器打开文件。 This helps Python clean up and close the file even if something goes wrong, and it's a good habit to get into.即使出现问题,这也有助于 Python 清理和关闭文件,这是一个好习惯。

  2. As is conventional in Python, we've called the file variable f .按照 Python 的惯例,我们将文件变量称为f fd will make someone reading your code think you're talking about a file descriptor , which is not the same thing. fd会让阅读你代码的人认为你在谈论文件描述符,这不是一回事。

  3. Since it holds a single line, we'll call the string variable line , not lines .由于它包含一行,我们将调用字符串变量line ,而不是lines

  4. Rather than a regex, we can just check whether "usb" is in the line somewhere (and we're converting the line to lower case before checking so that we'll catch "USB" in the original too.而不是正则表达式,我们可以只检查"usb"是否在某处的行中(并且我们在检查之前将行转换为小写,以便我们也能在原始行中捕获"USB"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM