How to access global variables from a class?

Дилян Палау зов Via Users <users-GSC0n/0aZo1d/SJB6HiN2Ni2O/[email protected]> Wed, 21 Aug 2024 14:16:25 +0300
Newsgroups gmane.comp.lang.groovy.user
Message-ID <[email protected]>
Hello,

OpenHAB 4.2.1 is written in Java and has a Groovy 4.0.11 plugin to programme logic.  It allows writing code outside of classes, which is executed immediately.  When I place a file openhab/jsr223/test.groovy:


  import org.slf4j.LoggerFactory
 
  logger = LoggerFactory.getLogger("l")
 
  class A {
    public A() {
      logger.info("L")
    }
  }
  A a = new A()


the system logs

2024-08-21 12:15:21.061 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script ‘/etc/openhab/automation/jsr223/test.groovy’: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: logger for class: A

How can I make logger visible within the methods of class A, without passing it as parameter to the constructor?

Also asked at https://community.openhab.org/t/groovy-how-to-access-global-variable-from-a-class/ .

Kind regards
  Дилян