简体   繁体   中英

Set line maximum of log file

Currently I write a simple logger to log messages from my bash script. The logger works fine and I simply write the date plus the message in the log file. Since the log file will increase, I would like to set the limit of the logger to for example 1000 lines. After reaching 1000 lines, it doesn't delete or totally clear the log file. It should truncate the first line and replace it with the new log line. So the file keeps 1000 lines and doesn't increase further. The latest line should always be at the top of the file. Is there any built in method? Or how could I solve this?

Your chosen example may not be the best. As the comments have already pointed out, logrotate is the best tool to keep log file sizes at bay; furthermore, a line is not the best unit to measure size. Those commenters are both right.

However, I take your question at face value and answer it.

You can achieve what you want by shell builtins, but it is much faster and simpler to use an external tool like sed . ( awk is another option, but it lacks the -i switch which simplifies your life in this case.)

So, suppose your file exists already and is named script.log then

maxlines=1000
log_msg='Whatever the log message is'

sed -i -e1i"\\$log_msg" -e$((maxlines))',$d' script.log

does what you want.

  • -i means modify the given file in place.
  • -e1i"\\\\$log_msg" means nsert $log_msg before the first ( ) line. 在第一( )行之前插入 $log_msg
  • -e$((maxlines))',$d' means elete each line from line number $((maxlines)) to the last one ( ). elete从行号的每一行 $((maxlines)) 到最后一个

Why would you want to replace the first line with the new message thereby causing a jump in the order of messages in your log file instead of just deleting the first line and appending the new message, eg simplistically:

log() {
    tail -999 logfile > tmp &&
    { cat tmp && printf '%s\n' "$*"; } > logfile
}

log "new message"

You don't even need a tmp file if your log file is always small lines, just save the output of the tail in a variable and printf that too.

Note that unlike a sed -i solution, the above will not change the inode, hardlinks, permissions or anything else for logfile - it's the same file as you started with just with updated content, it's not getting replaced with a new file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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