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?