title: Adding logger to Jenkins pipeline shared library class
body: <p>Trying to add logger to myClass. I want to see it in Jenkins console without using of "script.echo".</p>
  
  <pre><code>class myClass implements Serializable {
      def _logger
      def script
      myClass(def script, Map config) {
          _logger = script.getContext(TaskListener.class).getLogger()
          this.script = script
          this.config = config // some data
          log 'Initializing myClass...'
      }
  
      @NonCPS
      private void log(message) {
          _logger.println(message)
      }
  ...
  
  def someMethod(){
      ...
      script.sh(someScript)
      ...
  }
  }
  </code></pre>
  
  <p>With this code in Jenkins console I see:</p>
  
  <pre><code>Initializing myClass...                &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  [Pipeline] sh
  [test-job] Running shell script
  [Pipeline] }
  [Pipeline] // script
  [Pipeline] }
  [Pipeline] // stage
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  an exception which occurred:
      in field com.macys.devops.utils.myClass._logger
      in object com.macys.devops.utils.myClass@54e9341c
      in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
      ....
      in field com.cloudbees.groovy.cps.Continuable.e
      in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@4c564ac6
      in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
      ...
      in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@6b58d460
  Caused: java.io.NotSerializableException: java.io.PrintStream
  </code></pre>
  
  <p>When I comment </p>
  
  <blockquote>
    <p>this.script = script:</p>
  </blockquote>
  
  <p>logger works, but I can't execute DSL commands (e.g. script.sh).</p>
  
  <p>What I'm doing wrong?</p>