AWS CloudWatch 的正确格式
Correct format for AWS CloudWatch
我在 AWS ECS 上运行的 Docker 容器中使用 Servant and colog。
HTTP 请求已完美记录到 AWS CloudWatch,但不是我的手动记录。
我以这种方式启动了我的 WAI 服务器:
main :: IO ()
main = do
withStdoutLogger $ \aplogger -> do
let settings = setPort 8080 $ setLogger aplogger defaultSettings
initializeApplication >>= runSettings settings
我也尝试模仿 Apache Logging 风格:
logStringStdoutCloudWatch :: MonadIO m => LogAction m String
logStringStdoutCloudWatch = LogAction $ \x -> liftIO $ do
let format = "%d/%b/%Y:%T %z"
time <- getCurrentTime
putStrLn $ "- - - [" ++ formatTime defaultTimeLocale format time ++ "] \"log\" - - \"\" \"" ++ x ++ "\""
它给了我以下日志:
- - - [12/Nov/2020:16:36:16 +0000] "log" - - "" "A log message"
127.0.0.1 - - [12/Nov/2020:17:36:16 +0100] "POST /event HTTP/1.1" 400 - "" "Client"
但只有最后一行记录在 CloudWatch 中。
我的格式有什么问题?或者:是否有专门的 lib/function 来获得正确的格式?
实际上我已经作弊将日志格式更改为json,首先在Servant
级别:
import Network.Wai.Middleware
import Network.Wai.Middleware.RequestLogger
import Network.Wai.Middleware.RequestLogger.JSON(formatAsJSONWithHeaders)
initializeApplication :: IO Application
initializeApplication = do
logger <-
mkRequestLogger def {outputFormat = CustomOutputFormatWithDetailsAndHeaders formatAsJSONWithHeaders}
return
$ logger
$ serve (Proxy @API)
$ hoistServer (Proxy @API) (toHandler env) server
main :: IO ()
main = do
let settings = setPort 8080 defaultSettings
initializeApplication >>= runSettings settings
和记录器:
logStringStdoutCloudWatch :: MonadIO m => LogAction m String
logStringStdoutCloudWatch = LogAction $ \x -> liftIO $ do
let format = "%d/%b/%Y:%T %z"
time <- getCurrentTime
putStrLn $ "{\"time\":\"" ++ formatTime defaultTimeLocale format time ++ "\",\"log\":\"" ++ x ++ "\"}"
我在 AWS ECS 上运行的 Docker 容器中使用 Servant and colog。
HTTP 请求已完美记录到 AWS CloudWatch,但不是我的手动记录。
我以这种方式启动了我的 WAI 服务器:
main :: IO ()
main = do
withStdoutLogger $ \aplogger -> do
let settings = setPort 8080 $ setLogger aplogger defaultSettings
initializeApplication >>= runSettings settings
我也尝试模仿 Apache Logging 风格:
logStringStdoutCloudWatch :: MonadIO m => LogAction m String
logStringStdoutCloudWatch = LogAction $ \x -> liftIO $ do
let format = "%d/%b/%Y:%T %z"
time <- getCurrentTime
putStrLn $ "- - - [" ++ formatTime defaultTimeLocale format time ++ "] \"log\" - - \"\" \"" ++ x ++ "\""
它给了我以下日志:
- - - [12/Nov/2020:16:36:16 +0000] "log" - - "" "A log message"
127.0.0.1 - - [12/Nov/2020:17:36:16 +0100] "POST /event HTTP/1.1" 400 - "" "Client"
但只有最后一行记录在 CloudWatch 中。
我的格式有什么问题?或者:是否有专门的 lib/function 来获得正确的格式?
实际上我已经作弊将日志格式更改为json,首先在Servant
级别:
import Network.Wai.Middleware
import Network.Wai.Middleware.RequestLogger
import Network.Wai.Middleware.RequestLogger.JSON(formatAsJSONWithHeaders)
initializeApplication :: IO Application
initializeApplication = do
logger <-
mkRequestLogger def {outputFormat = CustomOutputFormatWithDetailsAndHeaders formatAsJSONWithHeaders}
return
$ logger
$ serve (Proxy @API)
$ hoistServer (Proxy @API) (toHandler env) server
main :: IO ()
main = do
let settings = setPort 8080 defaultSettings
initializeApplication >>= runSettings settings
和记录器:
logStringStdoutCloudWatch :: MonadIO m => LogAction m String
logStringStdoutCloudWatch = LogAction $ \x -> liftIO $ do
let format = "%d/%b/%Y:%T %z"
time <- getCurrentTime
putStrLn $ "{\"time\":\"" ++ formatTime defaultTimeLocale format time ++ "\",\"log\":\"" ++ x ++ "\"}"