title: Groovy + write log to file + Inject Logging Using Annotations
body: <p>I create the following groovy script in order to show how to inject a log field into our classes with a simple annotation</p>
  
  <pre><code>// File: LogSlf4j.groovy
  // Add dependencies for Slf4j API and Logback
  
  @Grapes([
  
         @Grab(group='org.slf4j', module='slf4j-api', version='1.6.1'),
         @Grab(group='ch.qos.logback', module='logback-classic', version='0.9.28')
  ])
  
   import org.slf4j.*
   import groovy.util.logging.Slf4j
  
   // Use annotation to inject log field into the class.
   @Slf4j
  
   class faimily{
  
   def father() {
  
  
      log.debug 'car engine is hot'
      log.error 'my car is stuck'
  }
  
  
      def mother() {
  
  
      log.debug 'dont have a water in the kitchen'
      log.error 'Cant make a cake'
  }
  
  
  }
  
   def helloWorld = new faimily()
   helloWorld.father()
   helloWorld.mother()
  </code></pre>
  
  <p>when I run the groovy script I get the following results ( on GROOVY CONSOLE )</p>
  
  <pre><code> 17:58:50.938 [Thread-59] DEBUG faimily - car engine is hot
   17:58:50.938 [Thread-59] ERROR faimily - my car is stuck
   17:58:50.938 [Thread-59] DEBUG faimily - dont have a water in the kitchen
   17:58:50.938 [Thread-59] ERROR faimily - Cant make a cake
  </code></pre>
  
  <p>please advice how we can print the results to a log file in the WIN machine , and what need to add to my groovy script in order to enable that?</p>
  
  <p>for example:</p>
  
  <p>the log file </p>
  
  <blockquote>
    <p>C:\Program Files\LOGS\my.groovy.log</p>
  </blockquote>
  
  <p>( should contain the results: )</p>
  
  <pre><code> 17:58:50.938 [Thread-59] DEBUG faimily - car engine is hot
   17:58:50.938 [Thread-59] ERROR faimily - my car is stuck
   17:58:50.938 [Thread-59] DEBUG faimily - dont have a water in the kitchen
   17:58:50.938 [Thread-59] ERROR faimily - Cant make a cake
  </code></pre>