Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read logs during program execution ? #56

Open
jfaure opened this issue Mar 19, 2020 · 1 comment
Open

How to read logs during program execution ? #56

jfaure opened this issue Mar 19, 2020 · 1 comment

Comments

@jfaure
Copy link

jfaure commented Mar 19, 2020

In order notably to return logging information if a client requests it

@StaelTchinda
Copy link

Hi @jfaure, if you're still looking for an answer, I found out that you can use handlers to either print it in the console or save it in a file. Here is an example from src/System/Log/Logger.hs

 import System.Log.Logger
 import System.Log.Handler.Syslog
 import System.Log.Handler.Simple
 import System.Log.Handler (setFormatter)
 import System.Log.Formatter

 -- By default, all messages of level WARNING and above are sent to stderr.
 -- Everything else is ignored.

 -- "MyApp.Component" is an arbitrary string; you can tune
 -- logging behavior based on it later.
 main = do
        debugM "MyApp.Component"  "This is a debug message -- never to be seen"
        warningM "MyApp.Component2" "Something Bad is about to happen."

        -- Copy everything to syslog from here on out.
        s <- openlog "SyslogStuff" [PID] USER DEBUG
        updateGlobalLogger rootLoggerName (addHandler s)

        errorM "MyApp.Component" "This is going to stderr and syslog."

        -- Now we'd like to see everything from BuggyComponent
        -- at DEBUG or higher go to syslog and stderr.
        -- Also, we'd like to still ignore things less than
        -- WARNING in other areas.
        --
        -- So, we adjust the Logger for MyApp.BuggyComponent.

        updateGlobalLogger "MyApp.BuggyComponent"
                           (setLevel DEBUG)

        -- This message will go to syslog and stderr
        debugM "MyApp.BuggyComponent" "This buggy component is buggy"

        -- This message will go to syslog and stderr too.
        warningM "MyApp.BuggyComponent" "Still Buggy"

        -- This message goes nowhere.
        debugM "MyApp.WorkingComponent" "Hello"

        -- Now we decide we'd also like to log everything from BuggyComponent at DEBUG
        -- or higher to a file for later diagnostics.  We'd also like to customize the
        -- format of the log message, so we use a 'simpleLogFormatter'

        h <- fileHandler "debug.log" DEBUG >>= \lh -> return $
                 setFormatter lh (simpleLogFormatter "[$time : $loggername : $prio] $msg")
        updateGlobalLogger "MyApp.BuggyComponent" (addHandler h)

        -- This message will go to syslog and stderr,
        -- and to the file "debug.log" with a format like :
        -- [2010-05-23 16:47:28 : MyApp.BuggyComponent : DEBUG] Some useful diagnostics...
        debugM "MyApp.BuggyComponent" "Some useful diagnostics..."

With a bit of reverse engineering, I think you can do it too ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants