# Combination of FormatLogger and FileLogger in LoggingExtras

**URL:** https://discourse.julialang.org/t/combination-of-formatlogger-and-filelogger-in-loggingextras/60654
**Category:** New to Julia
**Tags:** logging
**Created:** [May 6, 2021, 2:03pm UTC](https://discourse.julialang.org/t/combination-of-formatlogger-and-filelogger-in-loggingextras/60654 "2021-05-06T14:03:56Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [May 6, 2021, 2:03pm UTC](https://discourse.julialang.org/t/combination-of-formatlogger-and-filelogger-in-loggingextras/60654/1 "2021-05-06T14:03:56Z")

</div>

Is it possible to combine the capabilities of a `FormatLogger` and of a `FileLogger` in `LoggingExtras.jl`?

Quite obviously, the first one helps formatting logging messages with the other sends them to appropriate files. Though I can’t figure how to use both capabilities at the same time.

Here is an example of a logger using the package documentation

```julia
function init_logger()
   # fmt_logger = FormatLogger() do io,args
   # println(io,args._module," | ","[",args.level,"] ",args.message)
   # end
   logger = TeeLogger(
        global_logger(),
        MinLevelLogger(FileLogger("info.log"), Logging.Info),
        MinLevelLogger(FileLogger("warn.log"), Logging.Warn),
        MinLevelLogger(FileLogger("error.log"), Logging.Error),
   )
   return logger
end

logger = init_logger()
with_logger(logger) do
       @info "blablabla"
       @warn "bliblibli"
       @error "blobloblo"
end

```

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 6, 2021, 10:16pm UTC](https://discourse.julialang.org/t/combination-of-formatlogger-and-filelogger-in-loggingextras/60654/2 "2021-05-06T22:16:08Z")

</div>

All that `FileLogger` really does is to open the file and pass it to a `SimpleLogger`. So to use a “`FileLogger`” for `FormatLogger` you can just open the files yourself and pass the IO: `FormatLogger(fmt, open("info.log", "w"))`. Here is the full example:

```julia
function init_logger()
   function fmt(io, args)
       println(io, args._module, " | ", "[", args.level, "] ", args.message)
   end
   logger = TeeLogger(
        global_logger(),
        MinLevelLogger(FormatLogger(fmt, open("info.log", "w")), Logging.Info),
        MinLevelLogger(FormatLogger(fmt, open("warn.log", "w")), Logging.Warn),
        MinLevelLogger(FormatLogger(fmt, open("error.log", "w")), Logging.Error),
   )
   return logger
end

logger = init_logger()
with_logger(logger) do
       @info "blablabla"
       @warn "bliblibli"
       @error "blobloblo"
end

```

---

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [May 7, 2021, 7:21am UTC](https://discourse.julialang.org/t/combination-of-formatlogger-and-filelogger-in-loggingextras/60654/3 "2021-05-07T07:21:25Z")

</div>

Works like a charm, thanks !
