1
0

More than 3 years have passed since last update.

tail -fをカスタマイズするツールを作ってみた

Posted at

tail -fを実行すると、ログが延々と出力されますが、出力しつつ結果をカスタマイズするツールを作った。

  • 時刻表示

ログの時刻表示形式がマチマチだったり、場合によっては時刻が出ない場合もある。1行ずつ時刻を表示させるようにしてみた。
追加でエポック時刻も表示させ、処理時間を算出しやすくしてみた。

  • キーワードの色づけ

tailすると表示が流れてしまうので、色がつくようにしてみた。

実装

tailの実装は https://github.com/hpcloud/tail をインポートした。引数なしで実行するとこんな感じ。

NAME:
   decotail - decorated tail

USAGE:
   decotail [global options] command [command options] [arguments...]

VERSION:
   1.0

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   -t             (default: false)
   -k value       add color to text
   --help, -h     show help (default: false)
   --version, -v  print the version (default: false)

-tで時刻表示、-kで色づけするキーワードを指定する。("A B C")のようにスペース区切りで6キーワードまで色をつける。7キーワード目以降は色をつけない。
decotail -t -k "apple syslogd" /var/log/system.logを実行してみた。(色はわかりませんが...)

2020-01-03 16:32:49.312 1578036769 Jan  3 00:03:46 kuritayunoiMac syslogd[124]: ASL Sender Statistics
2020-01-03 16:32:49.312 1578036769 Jan  3 00:04:21 kuritayunoiMac GoogleSoftwareUpdateDaemon[5585]: objc[5585]: Class KSPaths is implemented in both /Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/Frameworks/DeviceManagement.framework/Versions/A/DeviceManagement (0x106578b48) and /Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/GoogleSoftwareUpdateDaemon (0x106365e50). One of the two will be used. Which one is undefined.
2020-01-03 16:32:49.313 1578036769 Jan  3 00:04:21 kuritayunoiMac GoogleSoftwareUpdateDaemon[5585]: objc[5585]: Class KSBundle is implemented in both /Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/Frameworks/DeviceManagement.framework/Versions/A/DeviceManagement (0x106578aa8) and /Library/Google/GoogleSoftwareUpdate/GoogleSoftwareUpdate.bundle/Contents/MacOS/GoogleSoftwareUpdateDaemon (0x106366210). One of the two will be used. Which one is undefined.
2020-01-03 16:32:49.313 1578036769 Jan  3 00:04:22 kuritayunoiMac com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.pid.mdmclient.5592): Failed to bootstrap path: path = /usr/libexec/mdmclient, error = 108: Invalid path
2020-01-03 16:32:49.313 1578036769 Jan  3 00:13:20 kuritayunoiMac AMPDeviceDiscoveryAgent[3028]: Entered:_AMMuxedDeviceDisconnected, mux-device:8
2020-01-03 16:32:49.313 1578036769 Jan  3 00:13:20 kuritayunoiMac AMPDeviceDiscoveryAgent[3028]: Entered:__thr_AMMuxedDeviceDisconnected, mux-device:8
2020-01-03 16:32:49.313 1578036769 Jan  3 00:13:20 kuritayunoiMac AMPDeviceDiscoveryAgent[3028]: tid:9e1b - Mux ID not found in mapping dictionary
2020-01-03 16:32:49.313 1578036769 Jan  3 00:13:20 kuritayunoiMac AMPDeviceDiscoveryAgent[3028]: tid:9e1b - Can't handle disconnect with invalid ecid
2020-01-03 16:32:49.313 1578036769 Jan  3 00:13:40 kuritayunoiMac AMPDeviceDiscoveryAgent[3028]: Entered:_AMMuxedDeviceDisconnected, mux-device:9
2020-01-03 16:32:49.313 1578036769 Jan  3 00:13:40 kuritayunoiMac AMPDeviceDiscoveryAgent[3028]: Entered:__thr_AMMuxedDeviceDisconnected, mux-device:9

https://github.com/kuritayu/infra-tools にツールをおいてます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0