-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLogger.hs
More file actions
41 lines (33 loc) · 1.16 KB
/
Logger.hs
File metadata and controls
41 lines (33 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Logger where
import System.Directory
import Data.Time.Clock
import Data.Time.Calendar
import Control.Applicative
import Data.Monoid
getDate :: IO String
getDate = dateToString . toGregorian . utctDay <$> getCurrentTime
where
dateToString (year, month, day) = mconcat [formattedYear, "-", formattedMonth, "-", formattedDay, ".txt"]
where
formattedDay = if day < 10
then "0" ++ show day
else show day
formattedMonth = if month < 10
then "0" ++ show month
else show month
formattedYear = show year
directory :: String
directory = "/home/serve/script/logs/"
logFile :: String -> String -> String
logFile prefix date = mconcat [directory, prefix, "-", date]
toLog :: String -> String -> IO ()
toLog prefix content = do
existence <- doesDirectoryExist directory
if not existence
then do
createDirectoryIfMissing False directory
date <- getDate
appendFile (logFile prefix date) (content ++ "\n")
else do
date <- getDate
appendFile (logFile prefix date) (content ++ "\n")