在Linux系统中监控JavaScript(Node.js)日志有多种工具和方法,以下是一些常用的工具和方案:
-f 选项可以实时监控日志文件的更新。tail -f /path/to/your/js/logfile.log
tail -f /path/to/your/js/logfile.log | grep 'error'
tail -f 实现实时查看。tail -f /path/to/your/js/logfile.log | less
pm2 logs myapi
npm install lawgslawgs /path/to/your/js/logfile.log
watchdog 库编写脚本监控日志文件变化并输出新内容。from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
class LogHandler(FileSystemEventHandler):
    def on_modified(self, event):
        with open(event.src_path, 'r') as file:
            content = file.readlines()
            print(content)
if __name__ == "__main__":
    path = '.'  # 监控当前目录
    filename = 'your-js-log-file.log'  # 要监控的日志文件名
    event_handler = LogHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=False)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
选择哪种工具取决于你的具体需求、应用规模和团队资源。例如,对于小型应用,PM2或基础日志方案可能足够;对于大型分布式系统,ELK或Loki+Grafana可能更合适。