・ 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)
Create and download the authentication information from Setting up authentication.
Rename the downloaded authentication information json file to "service.json" etc.
pip install google-cloud-logging
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')
Browse the logs on Google's Log Explorer (https://console.cloud.google.com/logs/).
Since you can follow the log graphically, it seems to be reasonably easy to use.
Recommended Posts