moca-log-file

The logging module of Moca System.


Keywords
Moca, MocaLogFile logging log, log, logging
License
OpenSSL
Install
pip install moca-log-file==1.3.2

Documentation

System Requirements

  • Python >= 3.7
  • moca_core
  • moca_config

Installation

pip install moca_log_file==1.3.0
or
pip install moca_log_file

Overview

English

This is the logging module developed by el.ideal-ideas for Moca System. All log message in the queue will be write to the log file. The log file will be changed automatically by date. one day in one file. The write-log-loop will be run on other thread or process. (can choose). So the response don't need to wait for file output. This module supports multiple language, MocaLogFile will select one language with your config to write.


日本語

これはモカシステムのためにel.ideal-ideasによって開発されたログモジュールである。 キューに格納されているすべてのログメッセージはログファイルに順番に書き込まれます。 ログファイルは自動的に日付に応じて切り替えられ、1日のログごと1つのファイルになります。 ログのファイル書き込みループは他のスレッドまたはプロセスで行われます。(選択可能)。 したがって、レスポンスはファイルの書き込みを待つ必要はありません。 このモジュールは複数言語をサポートしており、設定に応じて一つの言語を選んで書き込みます。


简体中文

这是el.ideal-ideas为茉客系统开发的日志模块。 列队内所有的日志信息会被按照顺序写入文件。 日志文件会根据日期自动切换。每一天的日志都会有一个单独的日志文件。 文件的写入处理会在其他的线程或者进程运行(可选)。 所有后续处理无需等待文件的写入处理。 本模块支持多语言日志,会根据设定自动选择一个语言写入文件。

Usage Example

# Create a instance.
# インスタンス化。
# 创建实例。
moca_log = MocaLogFile('sample_app', './', lang='english', log_level=1)

# If you want to use with MocaConfig Module to set language.
# MocaConfigモジュールを使用して言語設定することも可能です。
# 您也可以通过MocaConfig模块来设定语言。
moca_config = MocaConfig('test', './test/test.json')
moca_log = MocaLogFile('sample_app', './', config=moca_config, log_level=1)

# Create a instance with pathlib.
# インスタンス化 (pathlib使用)。
# 通过pathlib创建实例。
from pathlib import Path
path = Path('./')
moca_log = MocaLogFile('sample_app', path, lang='english', log_level=1)

# Put log message to queue,
# If the log_level in arguments higher than or equal to log_level of the instance.
# system will put one log message into queue with language config.
# If the language config is english, system will only put english message into queue.
# Ofcourse you can only use one language.
# And the %s will be replaced by args.
# If you call this method in a function, MocaLogFile will add the caller function name to log message.
# ログメッセージをキューに入れます。
# 引数として与えたlog_levelがインスタンス自体のlog_levelより高いか等しい場合。
# 言語設定に応じて、一つのログメッセージがキューに入れられます。
# 言語設定が英語の場合は、英語のみキューに入れられます。
# もちろん最初から一つの言語しか使用しないことも可能です。
# ログメッセージ内の%sはargsの値によって置き換えられます。
# 関数内からこのメソッドを呼びだした場合は呼び出し元の関数名もログにのこります。
# 添加日志信息到列队。
# 如果参数的log_level大于或等于实例本身的log_level的值。
# 会根据语言设定选择一个日志添加到列队。
# 如果实例的语言是英文,系统只会添加英文日志到列队。
# 当然,您也可以只是用一个语言。
# 日志内的%s会根据args的值进行替换。
# 如果在函数内调用这个方法,函数名也会被记录。
moca_log.insert_log(log_level=2, 
                    english='Some Warning Message, ID: %s',
                    japanese='警告文など、ID: %s', 
                    chinese='警告信息,ID: %s', 
                    args=(5901,))
moca_log.insert_log(log_level=2, 
                    english='Some Warning Message, ID: %s',
                    args=(5901,))

Output example

[Level]|PID: process id||Thread Identifier: thread identifier|{module name.caller name}<timestamp>log message
[レベル]|PID: プロセスID||Thread Identifier: スレッド識別子|{モジュール名.呼び出し元関数名}<タイムスタンプ>ログメッセージ
[等级]|PID: 进程ID||Thread Identifier: 线程识别子|{模块名.调用者名}<时间>日志信息


[WARNING]|PID: 65298||Thread Identifier: 4388531648|{Application.main}<2020-01-19 19:28:31.390132>Some Warning Message, ID: 5901
# The __init__ method in MocaLogFile class.
# MocaLogFileクラスのイニシャライザー
# MocaLogFile类的生成器。

def __init__(self,
             name: str,
             dir_path: Union[Path, str] = './',
             lang: str = '',
             config: Optional[MocaConfig] = None,
             mode: int = 0,
             log_message_queue: Optional[Queue] = None,
             log_level: int = 1):
    """
    :param name: the name of this logger.
    :param dir_path: log directory path.
    :param lang: selected language.
    :param config: if lang is empty string or other unknown value, try use config to get language info.
    :param mode: 0 is thread mode, 1 is process mode.
    :param log_message_queue: the log message queue.
    :param log_level: the level of log system.
    """
# Use LogLevel
from moca_log_file import LogLevel
moca_log.insert_log(log_level=LogLevel.INFO, 
                    english='Some Warning Message, ID: %s',
                    args=(5901,))
class LogLevel(int, Enum):
    """Log level"""
    ALL_LOG: int = -1
    DEBUG: int = 0
    INFO: int = 1
    WARNING: int = 2
    ERROR: int = 3
    SERIOUS_ERROR: int = 4
    SECURITY_ERROR: int = 5
    NO_LOG: int = 99

Public Methods

  • def change_language(lang: str) -> None:

    • Change the language of the instance,
    • If you use MocaConfig module,
    • the language config in json file will be changed.
    • インスタンスの言語設定を変更
    • MocaConfigを使用している場合、JSONファイル内の言語設定も変更されます。
    • 更改实例语言。如果使用了MocaConfig的话,JSON文件内的语言设定也会被更改。
  • def print_log(log_level: int = 1, func_name: str = '', module_name: str = '', english: str = '', japanese: str = '', chinese: str = '', other: str = '', args: tuple = ()) -> None:

    • Print log message to terminal. similar to insert_log method.
    • ログメッセージをターミナルに出力します。insert_logメソッドに似ています。
    • 输出日志到终端。和insert_log方法相似。
def print_log(self,
             log_level: int = 1,
             func_name: str = '',
             module_name: str = '',
             english: str = '',
             japanese: str = '',
             chinese: str = '',
             other: str = '',
             args: tuple = ()) -> None:
    """
    print log to console.
    :param log_level: only print log when this value is higher than this logger's level.
    :param func_name: caller name.
    :param module_name: module name.
    :param english: english version log message.
    :param japanese: japanese version log message.
    :param chinese: chinese version log message.
    :param other: other language version log message.
    :param args: this arguments use to replace the %s mark in log message.
    :return: None.
    For example
    moca_log.print_log(log_level=1,
                        english='English log ID: %s',
                        japanese='日本語ログID: %s',
                        chinese='中文日志ID: %s',
                        args=('1234',))
    """
  • def insert_log(log_level: int = 1, func_name: str = '', module_name: str = '', english: str = '', japanese: str = '', chinese: str = '', other: str = '', args: tuple = ()) -> None:
    • Put log message to queue
    • ログメッセージをキューに追加します。
    • 添加日志到列队。
def insert_log(self,
               log_level: int = 1,
               func_name: str = '',
               module_name: str = '',
               english: str = '',
               japanese: str = '',
               chinese: str = '',
               other: str = '',
               args: tuple = ()) -> None:
    """
    insert log message into log message queue.
    :param log_level: only print log when this value is higher than this logger's level.
    :param func_name: caller name.
    :param module_name: module name.
    :param english: english version log message.
    :param japanese: japanese version log message.
    :param chinese: chinese version log message.
    :param other: other language version log message.
    :param args: this arguments use to replace the %s mark in log message.
    :return: None
    """
  • def change_log_level(log_level: int) -> bool:

    • Change log level of the instance.
    • インスタンスのログレベルを変更。
    • 更改实例的日志等级。
  • def change_message_queue(log_message_queue: Queue) -> None:

    • Change the message queue.
    • メッセージキューを変更します。
    • 更改日志列队。

License

  • MIT License
Copyright 2020.1.17 <el.ideal-ideas: https://www.el-ideal-ideas.com>

Permission is hereby granted, free of charge, 
to any person obtaining a copy of this software 
and associated documentation files (the "Software"),
to deal in the Software without restriction, 
including without limitation the rights to use, copy, 
modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH 
THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Other Information