[GO] Get Cloud Logging available in Python in 10 minutes

For such people

・ I'm looking for a cloud logging tool ・ I have multiple remote PCs running and want to aggregate logs. ・ I want to use it for free (as of the posting date, up to 50GiB every month free)

Overview

  1. Download the credentials file
  2. Install Google's Python library
  3. Write trial log
  4. View log

1. Download the credentials file

Create and download the authentication information from Setting up authentication.

Rename the downloaded authentication information json file to "service.json" etc.

2. Install Google's Python library

pip install google-cloud-logging

3. Write trial log

Coding example

import os
import sys
from google.cloud import logging
from pathlib import Path

class Logger(object):

    def __init__(self, log_name):

        #If there is no log name, make an initialization error
        if log_name == None or log_name == '':
            print('ERROR : log_name is blank')
            sys.exit()
      
        parameter = {}
        parameter['project_name'] = '[Enter the name of the google project that created the authentication information ("My Project"Such)】'
        parameter['credential_path'] = '[Enter the relative path of the authentication information json file (if it is the same directory)"service.json"Such】'
        os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = str((Path(Path.cwd()))/parameter['credential_path'])
        logging_client = logging.Client()

        self.logger = logging_client.logger(log_name)

    #Write log level DEBUG
    def debug(self, text):
        self.logger.log_text(text, severity='DEBUG')
        print('Logged[DEBUG]: {}'.format(text))

    #Write log level INFO
    def info(self, text):
        self.logger.log_text(text, severity='INFO')
        print('Logged[INFO]: {}'.format(text))

    #Write log level WARNING
    def warning(self, text):
        self.logger.log_text(text, severity='WARNING')
        print('Logged[WARNING]: {}'.format(text))

    #Write log level ERROR
    def error(self, text):
        self.logger.log_text(text, severity='ERROR')
        print('Logged[ERROR]: {}'.format(text))

    #Write log level CRITICAL
    def critical(self, text):
        self.logger.log_text(text, severity='CRITICAL')
        print('Logged[CRITICAL]: {}'.format(text))


if __name__ == '__main__':

  logger = Logger('my-log')

  logger.debug('test debug')
  logger.info('test info')
  logger.warning('test warning')
  logger.error('test error')
  logger.critical('test critical')

4. Log viewing

Browse the logs on Google's Log Explorer (https://console.cloud.google.com/logs/). img.jpg

Miscellaneous feelings

Since you can follow the log graphically, it seems to be reasonably easy to use.

Recommended Posts

Get Cloud Logging available in Python in 10 minutes
Get date in Python
Get YouTube Comments in Python
Get last month in python
Get Terminal size in Python
Explicitly get EOF in python
Thorough logging strategy in Python
Make Opencv available in Python
Get Evernote notes in Python
Get Japanese synonyms in Python
Get Python scripts to run quickly in Cloud Run using responder
Get Leap Motion data in Python.
[Python] logging in your own module
Get data from Quandl in Python
Get the desktop path in Python
Get the script path in Python
Get, post communication memo in Python
Get the desktop path in Python
Get started with Python in Blender
Python Logging
Try logging in to qiita with Python
Get exchange rates from open exchange rates in Python
Get Suica balance in Python (using libpafe)
Get Google Fit API data in Python
How to get a stacktrace in python
Get Youtube data in Python using Youtube Data API
[Python] Pandas to fully understand in 10 minutes
Get battery level from SwitchBot in Python
Get a token for conoha in python
Get Started with TopCoder in Python (2020 Edition)
Get the EDINET code list in Python
Get Precipitation Probability from XML in Python
Get rid of DICOM images in Python
Get metric history from MLflow in Python
Get your own IP address in Python
Get started with Python in 30 minutes! Development environment construction & learn basic grammar
Get time series data from k-db.com in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
[Python] Get the files in a folder with Python
Get the weather in Osaka via WebAPI (python)
Meta-analysis in Python
Unittest in python
Get the caller of a function in Python
Get keystrokes during background execution in Python (windows)
Epoch in Python
Get multiple maximum keys in Python dictionary type
Get image URL using Flickr API in Python
Get Google Cloud Storage object list in Java
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
Get the X Window System window title in Python
N-Gram in Python
Programming in python