# How to get concrete class name from the parent?

**URL:** <https://forum.crystal-lang.org/t/how-to-get-concrete-class-name-from-the-parent/2562>\
**Category:** Help & Support\
**Created:** [September 26, 2020, 10:19am UTC](https://forum.crystal-lang.org/t/how-to-get-concrete-class-name-from-the-parent/2562 "2020-09-26T10:19:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nico](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/nico/32/692_2.png) [@nico](https://forum.crystal-lang.org/u/nico)\
**Post date:** [September 26, 2020, 10:19am UTC](https://forum.crystal-lang.org/t/how-to-get-concrete-class-name-from-the-parent/2562/1 "2020-09-26T10:19:36Z")

</div>

Hello,

I want to define a logger in a parent class to avoid having to redefine it in all child classes. However in the logs I see the name of the parent class not the child class.

Please, someone know how to get the concret class from a parent class?

Example:

> **[Carcin](https://play.crystal-lang.org/#/r/9r7n)**

It’s Parent displayed, not Child.

I have tried with `{{ @type.subclasses.last }}` but it does not seem to work on a multi-level hierarchy.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [September 26, 2020, 10:51am UTC](https://forum.crystal-lang.org/t/how-to-get-concrete-class-name-from-the-parent/2562/2 "2020-09-26T10:51:16Z")

</div>

Here you go:

```crystal
require "log"

abstract class Parent
  Log = ::Log.for self

  macro inherited
    Log = ::Log.for self

    def log
      Log
    end
  end

  def initialize
    log.info { "hello" }
  end

  def foo
    log.info { "foo" }
  end
end

class Child < Parent
end

class Deep < Child
end

Child.new.foo
Deep.new.foo

```

Note that I needed to define a method `log` inside each type. The reason is that if we just use the base methods referring to `Log`, these are statically bound to the type where they are used (Parent).  
That is, when you write `SomeType` in a method, it will always point to the type resolved in that method, never in a child. (this is the same as in Ruby)

---

<div class="post-metadata">

**Author:** ![nico](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/nico/32/692_2.png) [@nico](https://forum.crystal-lang.org/u/nico)\
**Post date:** [September 26, 2020, 11:23am UTC](https://forum.crystal-lang.org/t/how-to-get-concrete-class-name-from-the-parent/2562/3 "2020-09-26T11:23:46Z")

</div>

Oh well done! Thanks for the solution and the explanations.

To limit intermediate definitions, I added this (it works well):

```crystal
macro inherited
      {% unless @type.abstract? %}
        Log = ::Log.for self
      {% end %}
end

```
