# AUTOGENERATED! DO NOT EDIT! File to edit: ../../../nbs/services/console/base.ipynb.

# %% auto 0
__all__ = ['Console_ServiceConfig', 'ConsoleHandler']

# %% ../../../nbs/services/console/base.ipynb 1
import json
from typing import List

from dc_logger.client.base import ServiceHandler, ServiceConfig, LogEntry


# %% ../../../nbs/services/console/base.ipynb 2
from dc_logger.client.base import ServiceConfig, OutputMode
from dataclasses import dataclass

# %% ../../../nbs/services/console/base.ipynb 3
@dataclass
class Console_ServiceConfig(ServiceConfig):
    """Console-specific log configuration"""
    
    output_mode: OutputMode = "console"

    def validate_config(self) -> bool:
        return True

# %% ../../../nbs/services/console/base.ipynb 4
class ConsoleHandler(ServiceHandler):
    """Handler for console output"""

    async def _write_json(self, entry: LogEntry) -> dict:
        message = json.dumps(entry.to_dict(), indent=2, default=str)
        print(message)
        return message


    async def _write_text(self, entry: LogEntry) -> str:
        message = f"[{entry.timestamp}] {entry.level.value} {entry.logger}: {entry.message}"
        print(message)
        return message
                

    async def write(self, entry: LogEntry, output_type) -> bool:
        """Write entries to console"""
        
        try:
            if output_type == "json":
                return self._write_json(entry)

            if output_type == "text":
                return self._write_text(entry)
                
        except Exception as e:
            print(f"Error writing to console: {e}")
            return False

    async def flush(self) -> bool:
        """Console output doesn't need flushing"""
        return True

