title: Logging in GCP and locally
body: <p>I am building a system that is intended to run on Virtual Machines in Google Cloud Platform. However, as a form of backup, it may be run locally as well. That being said, my issue currently is with logging. I have two loggers, both work, a local logger and a cloud logger. </p>
  
  <h3>Cloud logger</h3>
  
  <pre><code>import google.cloud.logging
  from google.cloud.logging.handlers import CloudLoggingHandler
  from google.oauth2 import service_account
  
  CREDS = google.cloud.logging.Client(
          project=PROJECT, credentials=service_account.Credentials.from_service_account_file(CREDENTIAL_FILE))
  
  class GoogleLogger(CloudLoggingHandler):
  
      def __init__(self, client=CREDS):
          super(GoogleLogger, self).__init__(client)
  
  def setup_logging():
      """
      This function can be invoked in order to setup logging based on a yaml config in the 
      root dir of this project
      """
      try:
          # with open('logging.yaml', 'rt') as f:
          with open(LOGGING_CONFIG, 'rt') as f:
              config = yaml.safe_load(f.read())
              f.close()
          logging.config.dictConfig(config)
      except Exception:
          print('Error in Logging Configuration. Using default configs')
          print(traceback.format_exc())
          logging.basicConfig(level=logging.INFO)
  </code></pre>
  
  <h3>logging.yaml</h3>
  
  <pre><code>version: 1
  
  formatters:
      simple:
          format: "%(name)s - %(lineno)d -  %(message)s"
  
      complex:
          format: "%(asctime)s - %(name)s | %(levelname)s | %(module)s : [%(filename)s: %(lineno)d] - %(message)s"
  
      json:
          class: logger.JsonFormatter
  
  handlers:
      console:
          class: logging.StreamHandler
          level: DEBUG
          formatter: complex
  
      cloud:
          class: logger.GoogleLogger
          formatter: json
          level: INFO
  
  loggers:
  
      cloud:
          level: INFO
          handlers: [console,cloud]
          propagate: yes
  
      __main__:
          level: DEBUG
          handlers: [console]
          propagate: yes
  </code></pre>
  
  <p>I use <code>setup_logging()</code> to set everything up like so:</p>
  
  <pre><code>setup_logging()
  logger = logging.getLogger(&lt;type_of_logger&gt;)
  </code></pre>
  
  <p> can be <code>"cloud"</code> or <code>"__main__"</code></p>
  
  <p>"<strong>main</strong>" only logs locally, "cloud" logs both to GCP Stackdriver Logs and locally.</p>
  
  <p>Now if I'm not running on GCP, an error gets thrown here:</p>
  
  <pre><code>CREDS = google.cloud.logging.Client(
          project=PROJECT, credentials=service_account.Credentials.from_service_account_file(CREDENTIAL_FILE))
  </code></pre>
  
  <p>What is the best way around this? The <code>class GoogleLogger(CloudLoggingHandler):</code> always gets run, and if isn't in GCP it breaks. </p>
  
  <p>An idea is to wrap the class in a <code>try/except</code> block, but that sounds like a horrible idea. How do I make my code smart enough to choose which logger automatically? And if running locally, completely ignore the <code>GoogleLogger</code>?</p>
  
  <h3>Edit (Traceback)</h3>
  
  <pre><code>File "import_test.py", line 2, in &lt;module&gt;
      from logger import setup_logging
    File "/Users/daudn/Documents/clean_space/tgs_workflow/utils/logger.py", line 16, in &lt;module&gt;
      class GoogleLogger(CloudLoggingHandler):
    File "/Users/daudn/Documents/clean_space/tgs_workflow/utils/logger.py", line 23, in GoogleLogger
      project=PROJECT, credentials=service_account.Credentials.from_service_account_file(CREDENTIAL_FILE))
    File "/usr/local/lib/python3.7/site-packages/google/cloud/logging/client.py", line 123, in __init__
      self._connection = Connection(self, client_info=client_info)
    File "/usr/local/lib/python3.7/site-packages/google/cloud/logging/_http.py", line 39, in __init__
      super(Connection, self).__init__(client, client_info)
  TypeError: __init__() takes 2 positional arguments but 3 were given
  </code></pre>