# LoggingExtras.jl - How to add custom log macro

**URL:** https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679
**Category:** General Usage
**Created:** [July 15, 2021, 9:29am UTC](https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679 "2021-07-15T09:29:38Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)
#### Post date: [July 15, 2021, 9:29am UTC](https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679/1 "2021-07-15T09:29:38Z")

</div>

@debug, @info, @warn, @error are by default, I would like to add log macro, f.e. @fatal. How can I do it with LoggingExtras.jl? @fredrikekre Thank you

I only tried following code (did not work for LoggingExtras.jl):

```julia
macro fatal(exprs...)
    quote
       @logmsg LogLevel(100) $(map(x -> esc(x), exprs)...)
   end
end
@fatal "My custom"

```

---

<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: [July 15, 2021, 9:36am UTC](https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679/2 "2021-07-15T09:36:12Z")

</div>

Perhaps just use what `@info` etc do: [julia/logging.jl at 468b1571b3676528feb1e44fd497b3bb9b55ef42 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/blob/468b1571b3676528feb1e44fd497b3bb9b55ef42/base/logging.jl#L258)

---

<div class="post-metadata">

### Author: ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)
#### Post date: [July 15, 2021, 10:00am UTC](https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679/3 "2021-07-15T10:00:41Z")

</div>

@fredrikekre Thank you for you advice, but I already tried it. It didn’t worked.

Following code doesn’t work:

```julia
macro fatal(exs...)
    logmsg_code((@_sourceinfo)..., :Fatal, exs...)
end

```

Output was:

```julia
UndefVarError: @_sourceinfo not defined

```

How should I implement it?

---

<div class="post-metadata">

### Author: ![LinasBa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/linasba/32/20957_2.png) [@LinasBa](https://discourse.julialang.org/u/LinasBa)
#### Post date: [July 15, 2021, 12:26pm UTC](https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679/4 "2021-07-15T12:26:08Z")

</div>

I haven’t really looked at the issue, but I believe error is thrown since `@_sourceinfo` is not exported.

```julia
macro fatal(exs...)
    logmsg_code((Base.CoreLogging.@_sourceinfo)..., :Fatal, exs...)
end

```

---

<div class="post-metadata">

### Author: ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)
#### Post date: [July 15, 2021, 12:39pm UTC](https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679/5 "2021-07-15T12:39:01Z")

</div>

@LinasBa Thank you so much, now I’m closer to the goal:

```julia
using Logging, LoggingExtras

macro fatal(exs...)
    Base.CoreLogging.logmsg_code((Base.CoreLogging.@_sourceinfo)..., :Info, exs...)
end

@fatal "My message"

```

Output:

```julia
UndefVarError: Info not defined

```

Any ideas?

---

<div class="post-metadata">

### Author: ![LinasBa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/linasba/32/20957_2.png) [@LinasBa](https://discourse.julialang.org/u/LinasBa)
#### Post date: [July 15, 2021, 12:50pm UTC](https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679/6 "2021-07-15T12:50:27Z")

</div>

You should probably provide module name for `:Info` as well.

```julia
Base.CoreLogging.:Info

```

But this gets new error thrown.

---

<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: [July 15, 2021, 1:31pm UTC](https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679/7 "2021-07-15T13:31:02Z")

</div>

This was much more complicated than I thought, but this seems to work correctly with all escaping and location information

```julia
function maybe_make_kwargs(ex)
    if ex isa Expr && ex.head === :(=) ||
       ex isa Expr && ex.head === :...
        return ex
    else
        Expr(:(=), ex, esc(ex))
    end
end

macro fatal(exs...)
    location = LineNumberNode( __source__.line, __source__.file)
    level = Base.CoreLogging.LogLevel(1001)
    message = esc(exs[1])
    kwargs = map(maybe_make_kwargs, exs[2:end])
    Expr(:macrocall, Base.CoreLogging.var"@logmsg", location, level, message, kwargs...)
end

```

Testing:

```julia-repl
julia> x = 123;

julia> @fatal "hello" x y = 456
┌ LogLevel(1001): hello
│ x = 123
│ y = 456
└ @ Main REPL[4]:1

julia> function f()
           x = 678
           @fatal "hello from f" x y = 321
       end;

julia> f()
┌ LogLevel(1001): hello from f
│ x = 678
│ y = 321
└ @ Main REPL[5]:3

julia> module A
           import ..Main: @fatal
           a = 111
           g() = @fatal "hello from module A $a" a y = 444
       end;

julia> A.g()
┌ LogLevel(1001): hello from module A 111
│ a = 111
│ y = 444
└ @ Main.A REPL[7]:4

```

---

<div class="post-metadata">

### Author: ![Michal](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Michal](https://discourse.julialang.org/u/Michal)
#### Post date: [July 16, 2021, 5:23am UTC](https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679/8 "2021-07-16T05:23:19Z")

</div>

Thank you very much. It is working. I would like to add small question. Is it possible to change macro name in console output? LogLevel(1001) → Fatal

Current:

```julia
┌ LogLevel(1001): hello from f
│ x = 678
│ y = 321
└ @ Main REPL[5]:3

```

Desired:

```julia
┌ Fatal: hello from f
│ x = 678
│ y = 321
└ @ Main REPL[5]:3

```

---

<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: [July 16, 2021, 7:34am UTC](https://discourse.julialang.org/t/loggingextras-jl-how-to-add-custom-log-macro/64679/9 "2021-07-16T07:34:34Z")

</div>

See [JuliaLang/julia#33418](https://github.com/JuliaLang/julia/issues/33418). Currently you have to do that yourself in a `FormatLogger` or similar:

```julia
julia> using LoggingExtras, Logging

julia> logger = FormatLogger(stderr) do io, args
           levelstr = args.level == Logging.LogLevel(100) ? "Fatal" : string(args.level)
           println(io, levelstr, ": ", args.message)
       end;

julia> global_logger(logger);

julia> @logmsg LogLevel(100) "hello"
Fatal: hello

```
