0
import logging
import os
from pathlib import Path
from pydantic import BaseModel, Field, field_validator

# Configure logging
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)

# Get environment variable
emvm = os.getenv("Backhole_environment", "").lower()

class YamlFileModel(BaseModel):  # Fixed class name
    object_path: Path = Field(..., description="Path to the YAML file")

    @field_validator('object_path', mode='before')
    def validate_object_path(cls, v):
        return Path(v).expanduser().resolve() if not isinstance(v, Path) else v.expanduser().resolve()

    @staticmethod
    def setup_yaml_path():
        yaml_directory = os.path.dirname(os.path.abspath(__file__))
        setting_dir = os.path.join(yaml_directory, 'settings')
        os.makedirs(setting_dir, exist_ok=True)

        filename = 'settings.yaml'
        yaml_settings_path = os.path.join(setting_dir, filename)

        if not os.path.exists(yaml_settings_path):
            log.error(f"YAML file not found at {yaml_settings_path}")
            with open(yaml_settings_path, 'w') as yaml_file:
                yaml_file.write("# Default YAML settings\n")

        if os.path.exists(yaml_settings_path) and os.path.getsize(yaml_settings_path) == 0:
            log.error(f"YAML file at {yaml_settings_path} is empty.")
            with open(yaml_settings_path, 'w') as yaml_file:
                yaml_file.write("default_key: default_value\n")

        if not os.access(yaml_settings_path, os.R_OK):
            log.error(f"YAML file at {yaml_settings_path} is not readable. Check file permissions.")

# Call setup_yaml_path() correctly
YamlFileModel.setup_yaml_path()

I want to make use of this by writing the data in a clearer way than JSON. The total goal is when you connect to your friends' game server YAML files list who is connected to your server, their IP, and where the connection is from basically writing logs including the errors. For now, the script only creates the YAML file in a given directory. How should I make it write the logs in the specific format I set?

This is the format I expect:

list connections [
server:
  host:
  addr: 

database:
  user: "admin"
  password: "securepass"
  name: "mydatabase"

features:
  enableLogging: true
  enableDebug: false

]

Can you point me to how should I enable this kind of logging? We made our own server in Fallout! Can you gave me some tips?

5
  • 1
    I don't understand whether you want to create log files which are in YAML format, or you want to use YAML files to configure the logging system, or you want to create log messages about reading a YAML configuration file (which configures something else). Commented Mar 23 at 10:04
  • Yes, I want to use the file to configure a logging system. Commented Mar 23 at 16:59
  • Then your code example is confusing, because you are already using the logging system while accessing the YAML file. And also, the code writes the YAML file which seems odd if it is a configuration file. Commented Mar 23 at 17:26
  • What could I do to fix it? Commented Mar 23 at 18:06
  • (1) do not write the file but read it, (2) instead of using a fixed logging configuration, use the data read from the YAML file to configure it Commented Mar 23 at 19:06

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.