logger icon indicating copy to clipboard operation
logger copied to clipboard

Rotating while live?

Open joe9 opened this issue 7 years ago • 1 comments

Hello,

Just want to check if it is possible for the logs to be rotated while live?

----------------------------------------
-- my usage of fast-logger
---------------------------------------
--   1 MiB = 1 mebibyte = 1,0242 bytes = 1,048,576 bytes
--   100 MiB = 1,048,576 * 100 bytes
logger :: Text -> TChan Text -> IO ()
logger logPrefix chan =
  do filename <- logFilename logPrefix
     let fileLogSpec =
           FileLogSpec (cs filename)
                       104857600
                       10
     withFastLogger (LogFile fileLogSpec 1048576)
                    (readAndLog fileLogSpec chan)

readAndLog
  :: FileLogSpec -> TChan Text -> (LogStr -> IO a) -> IO ()
readAndLog fileLogSpec chan f =
  atomically (readTChan chan) >>= f . (toLogStr :: Text -> LogStr) >>
  readAndLog fileLogSpec chan f
--------------------------------------------------

-- not sure how to use this. If I use it in readAndLog, it gives this
-- message
-- exe: /tmp/<filename.log>: openFile: resource busy (file is locked)
checkAndRotate :: FileLogSpec -> IO ()
checkAndRotate spec =
  do size <- getFileSize (log_file spec)
     when (size > log_file_size spec)
          (rotate spec)

getFileSize
  :: BasicPrelude.FilePath -> IO Integer
getFileSize path = withFile path ReadMode hFileSize

I have a log running process and it would be nice if fast-logger can auto-rotate the logs on reaching a certain size. The log filehandle is locked by fast-logger and I cannot read the size of the log file while fast-logger is running.

Thanks

joe9 avatar Aug 22 '16 13:08 joe9