### Run Log Effect with Standard Output Backend in Haskell Source: https://context7.com/haskell-effectful/log-effectful/llms.txt Demonstrates how to run the `Log` effect using `runEff` and `withStdOutLogger`. This example shows basic logging at different severity levels (INFO, DEBUG, WARNING, ERROR) to the standard output. ```haskell {-# LANGUAGE OverloadedStrings #-} module Main where import Effectful import Effectful.Log import Log.Backend.StandardOutput main :: IO () main = runEff $ do withStdOutLogger $ \logger -> do runLog "main" logger defaultLogLevel $ do logInfo_ "Application started" logDebug_ "Debug information" logWarning_ "A warning message" logError_ "An error occurred" -- Output: -- [2024-01-15 10:30:45] main INFO: Application started -- [2024-01-15 10:30:45] main DEBUG: Debug information -- [2024-01-15 10:30:45] main WARNING: A warning message -- [2024-01-15 10:30:45] main ERROR: An error occurred ``` -------------------------------- ### Logging to Standard Output and Elasticsearch in Haskell Source: https://github.com/haskell-effectful/log-effectful/blob/master/README.md This example demonstrates how to configure and use the log-effectful library to send log messages to both standard output and an Elasticsearch instance. It requires the effectful, log-effectful, and specific backend libraries. The function takes Elasticsearch configuration and logger actions as input and outputs log messages. ```haskell {-# LANGUAGE OverloadedStrings #-} module Main where import Effectful import Effectful.Log import Log.Backend.ElasticSearch import Log.Backend.StandardOutput main :: IO () main = runEff $ do let config = defaultElasticSearchConfig { esServer = "http://localhost:9200" , esIndex = "logs" } withStdOutLogger $ \stdoutLogger -> do withElasticSearchLogger config $ \esLogger -> do runLog "main" (stdoutLogger <> esLogger) defaultLogLevel $ do logInfo_ "Hi there" ``` -------------------------------- ### Use Log Effect Constraint in Haskell Functions Source: https://context7.com/haskell-effectful/log-effectful/llms.txt Demonstrates how to use the `Log :> es` constraint to define functions that require logging capabilities. This promotes modularity and testability by making effect dependencies explicit. The example shows `processOrder` and `validateOrder` functions that utilize logging within a scoped domain and context. ```haskell {-# LANGUAGE OverloadedStrings #-} module Main where import Effectful import Effectful.Log import Log.Backend.StandardOutput -- Function with explicit logging effect constraint processOrder :: Log :> es => Int -> Eff es () processOrder orderId = localDomain "orders" $ do localData ["order_id" .= orderId] $ do logInfo_ "Processing order" validateOrder orderId logInfo_ "Order processed successfully" validateOrder :: Log :> es => Int -> Eff es () validateOrder orderId = do logDebug "Validating order" $ object ["order_id" .= orderId] -- validation logic here logDebug_ "Order validation passed" main :: IO () main = runEff $ do withStdOutLogger $ \logger -> do runLog "ecommerce" logger defaultLogLevel $ do processOrder 12345 processOrder 67890 ``` -------------------------------- ### Hierarchical Logging Domains with localDomain in Haskell Source: https://context7.com/haskell-effectful/log-effectful/llms.txt Demonstrates the use of `localDomain` to create nested logging domains, enabling hierarchical organization of log messages. This is useful for tracing execution flow across different application components. ```haskell {-# LANGUAGE OverloadedStrings #-} module Main where import Effectful import Effectful.Log import Log.Backend.StandardOutput main :: IO () main = runEff $ do withStdOutLogger $ \logger -> do runLog "myapp" logger defaultLogLevel $ do logInfo_ "Starting application" localDomain "database" $ do logInfo_ "Connecting to database" localDomain "migrations" $ do logInfo_ "Running migrations" -- Domain: myapp.database.migrations localDomain "http" $ do logInfo_ "Starting HTTP server" -- Domain: myapp.http -- Output shows hierarchical domain paths: myapp, myapp.database, myapp.database.migrations, myapp.http ``` -------------------------------- ### Structured Logging with JSON Payloads in Haskell Source: https://context7.com/haskell-effectful/log-effectful/llms.txt Illustrates structured logging using `logInfo` and `logError` with JSON payloads. This allows for richer, machine-readable log data alongside plain text messages, enhancing observability. ```haskell {-# LANGUAGE OverloadedStrings #-} module Main where import Effectful import Effectful.Log import Log.Backend.StandardOutput main :: IO () main = runEff $ do withStdOutLogger $ \logger -> do runLog "api-server" logger defaultLogLevel $ do -- Simple message logging logInfo_ "Server starting" -- Structured logging with JSON payload logInfo "Request received" $ object ["method" .= ("GET" :: String) ,"path" .= ("/users" :: String) ,"status" .= (200 :: Int) ] -- Error logging with context logError "Database connection failed" $ object ["host" .= ("localhost" :: String) ,"port" .= (5432 :: Int) ,"retry_count" .= (3 :: Int) ] -- Output includes structured JSON data alongside log messages ``` -------------------------------- ### Scoping Contextual Data with localData in Haskell Source: https://context7.com/haskell-effectful/log-effectful/llms.txt Shows how to use `localData` to add contextual information to log messages within a specific scope. Any log generated within the `localData` block will automatically include the provided key-value pairs. ```haskell {-# LANGUAGE OverloadedStrings #-} module Main where import Effectful import Effectful.Log import Log.Backend.StandardOutput main :: IO () main = runEff $ do withStdOutLogger $ \logger -> do runLog "app" logger defaultLogLevel $ do -- Add request-scoped data localData ["request_id" .= ("abc-123" :: String), "user_id" .= (42 :: Int)] $ do logInfo_ "Processing request" -- All logs in this block automatically include request_id and user_id localData ["operation" .= ("create" :: String)] $ do logInfo_ "Creating resource" -- This log includes request_id, user_id, AND operation -- Output shows nested contextual data in each log entry ``` -------------------------------- ### Combine Multiple Logging Backends in Haskell Source: https://context7.com/haskell-effectful/log-effectful/llms.txt Combines multiple logging backends (e.g., StandardOutput and ElasticSearch) using the Monoid instance for `Logger`. Logs are simultaneously sent to all configured backends. This enables flexible log aggregation for different environments. Dependencies include `Effectful`, `Effectful.Log`, and specific backend modules. ```haskell {-# LANGUAGE OverloadedStrings #-} module Main where import Effectful import Effectful.Log import Log.Backend.ElasticSearch import Log.Backend.StandardOutput main :: IO () main = runEff $ do let esConfig = defaultElasticSearchConfig { esServer = "http://localhost:9200" , esIndex = "application-logs" } withStdOutLogger $ \stdoutLogger -> do withElasticSearchLogger esConfig $ \esLogger -> do -- Combine loggers - logs go to both stdout AND Elasticsearch runLog "production-app" (stdoutLogger <> esLogger) defaultLogLevel $ do logInfo "User login" $ object ["user_id" .= (12345 :: Int) ,"ip_address" .= ("192.168.1.100" :: String) ] logWarning "Rate limit approaching" $ object ["current_rate" .= (95 :: Int) ,"limit" .= (100 :: Int) ] ``` -------------------------------- ### Adjust Local Log Level in Haskell Source: https://context7.com/haskell-effectful/log-effectful/llms.txt Temporarily changes the maximum log level within a specific code block using `localMaxLogLevel`. This is useful for enabling verbose logging in targeted sections of your Haskell application without affecting the global log level. It requires the `Effectful` and `Effectful.Log` libraries. ```haskell {-# LANGUAGE OverloadedStrings #-} module Main where import Effectful import Effectful.Log import Log.Backend.StandardOutput main :: IO () main = runEff $ do withStdOutLogger $ \logger -> do -- Start with INFO level (debug messages filtered out) runLog "app" logger LogInfo $ do logInfo_ "This is logged" logDebug_ "This is NOT logged (below threshold)" -- Temporarily enable debug logging localMaxLogLevel LogDebug $ do logDebug_ "This IS logged (threshold lowered)" logTrace_ "This is NOT logged (still below debug)" logDebug_ "This is NOT logged again (back to INFO)" ``` -------------------------------- ### Define Log Effect in Haskell Source: https://context7.com/haskell-effectful/log-effectful/llms.txt Defines the core `Log` effect type for logging capabilities within the `MonadLog` typeclass. It includes operations for logging messages with severity and structured data, and for scoping log context. ```haskell data Log :: Effect where LogMessageOp :: LogLevel -> Text -> Value -> Log m () LocalData :: [Pair] -> m a -> Log m a LocalDomain :: Text -> m a -> Log m a LocalMaxLogLevel :: LogLevel -> m a -> Log m a GetLoggerEnv :: Log m LoggerEnv ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.