VOOZH about

URL: https://deepwiki.com/hypervel/log/4.2-factory-adapters

⇱ Factory Adapters | hypervel/log | DeepWiki


Loading...
Menu

Factory Adapters

Purpose and Scope

This document explains the factory adapter components (HyperfLogFactory and LogFactoryAdapter) that integrate the hypervel/log package with Hyperf's native LoggerFactory pattern. These adapters provide an alternative access mechanism for obtaining logger instances, complementing the direct LoggerInterface dependency injection pattern.

For information about the primary service registration and dependency injection binding (LoggerInterfaceLogManager), see ConfigProvider & Service Registration. For details about the underlying LogManager that these adapters wrap, see LogManager: Channel Factory & Manager.

Adapter Architecture Overview

The factory adapter system consists of two classes that work together to bridge Hyperf's LoggerFactory interface with the hypervel/log LogManager:

ComponentTypePurposeLocation
HyperfLogFactoryFactoryCreates LogFactoryAdapter instances via dependency injectionsrc/Adapter/HyperfLogFactory.php
LogFactoryAdapterAdapterExtends Hyperf\Logger\LoggerFactory and delegates to LogManagersrc/Adapter/LogFactoryAdapter.php

Adapter Pattern Relationship


Sources: src/Adapter/HyperfLogFactory.php1-19 src/Adapter/LogFactoryAdapter.php1-23

HyperfLogFactory

The HyperfLogFactory class is a simple factory that creates LogFactoryAdapter instances through Hyperf's dependency injection container.

Class Structure


Sources: src/Adapter/HyperfLogFactory.php10-18

Implementation Details

The HyperfLogFactory::__invoke() method follows the Hyperf factory pattern:

Factory Method Signature: src/Adapter/HyperfLogFactory.php12-18

public function __invoke(ContainerInterface $container): LogFactoryAdapter

The factory performs two key operations:

  1. Retrieves ConfigInterface: Obtains the configuration service from the container src/Adapter/HyperfLogFactory.php16
  2. Instantiates LogFactoryAdapter: Creates and returns a new adapter instance with both dependencies src/Adapter/HyperfLogFactory.php14-17

This factory is typically invoked by Hyperf's DI container when resolving the LoggerFactory binding, though it can also be used manually if needed.

Sources: src/Adapter/HyperfLogFactory.php10-18

LogFactoryAdapter

The LogFactoryAdapter class extends Hyperf's LoggerFactory abstract class and adapts its interface to work with the hypervel/log LogManager.

Inheritance Chain


Sources: src/Adapter/LogFactoryAdapter.php10

Method Implementations

The LogFactoryAdapter overrides two methods from the parent LoggerFactory class:

MethodParametersReturn TypePurpose
make()$name, $groupLoggerInterfaceCreates/retrieves a logger instance
get()$name, $groupLoggerInterfaceAlias for make()

Parameter Handling: Both methods accept $name and $group parameters to match the parent interface signature, but these parameters are not used in the implementation. This is explicitly documented in the code comments src/Adapter/LogFactoryAdapter.php12-18:

// name and group parameters are not applicable here

Delegation Pattern

Both make() and get() methods follow the same delegation pattern:

Method Flow


Sources: src/Adapter/LogFactoryAdapter.php13-22

Implementation Logic

make() Method: src/Adapter/LogFactoryAdapter.php13-16

public function make($name = 'hyperf', $group = 'default'): LoggerInterface
{
 return $this->container->get(LoggerInterface::class)->channel();
}

get() Method: src/Adapter/LogFactoryAdapter.php19-22

public function get($name = 'hyperf', $group = 'default'): LoggerInterface
{
 return $this->container->get(LoggerInterface::class)->channel();
}

Both methods:

  1. Resolve LoggerInterface from the container (which returns the LogManager instance)
  2. Call channel() with no arguments to get the default channel
  3. Return the resulting LoggerInterface (a Logger instance)

The channel() method is defined in LogManager src/LogManager.php93-96 and delegates to driver(), which resolves the default channel from configuration src/LogManager.php486-489

Sources: src/Adapter/LogFactoryAdapter.php13-22 src/LogManager.php93-104

Usage Patterns

Direct LogManager Access vs Factory Adapter


Sources: src/Adapter/LogFactoryAdapter.php13-22 src/LogManager.php93-104

When to Use Each Pattern

Access PatternUse CaseAdvantagesDisadvantages
Direct LoggerInterface InjectionNew applications, explicit channel selectionFull access to LogManager methods, explicit channel control, supports custom channelsRequires understanding of channel concept
LoggerFactory AdapterMigrating from Hyperf's default logger, legacy code compatibilityFamiliar Hyperf pattern, minimal code changesAlways uses default channel, ignores name/group parameters

Code Examples

Pattern 1: Direct LoggerInterface Injection


Pattern 2: Factory Adapter


Sources: src/Adapter/LogFactoryAdapter.php13-22

Container Registration

The factory adapter is typically registered in the DI container through the ConfigProvider. While the primary binding is LoggerInterfaceLogManager, applications can optionally register the factory adapter for compatibility with Hyperf's LoggerFactory pattern.

Registration Example


Once registered, the container will invoke HyperfLogFactory::__invoke() when resolving LoggerFactory, which creates and returns a LogFactoryAdapter instance.

Sources: src/Adapter/HyperfLogFactory.php12-18

Design Rationale

The factory adapter system exists to provide backward compatibility with Hyperf applications that use the framework's native LoggerFactory pattern. Key design decisions:

ParameterIgnoring

The $name and $group parameters are deliberately ignored src/Adapter/LogFactoryAdapter.php12-18 because:

  1. Channel Configuration: The hypervel/log package uses a different channel configuration system based on the logging.php configuration file (see Log Channels & Drivers)
  2. Default Channel Pattern: The package's design favors explicit channel selection via LogManager::channel($name) rather than factory parameters
  3. Interface Compliance: The parameters must exist to satisfy the parent LoggerFactory interface contract

Delegation to LogManager

Both methods delegate to LogManager::channel() without arguments src/Adapter/LogFactoryAdapter.php15-21 which:

  1. Uses the default channel from configuration src/LogManager.php486-489
  2. Maintains consistency with the package's primary access pattern
  3. Ensures all logging goes through the same configuration and channel resolution logic

Sources: src/Adapter/LogFactoryAdapter.php10-23 src/LogManager.php93-489

Class Relationships Summary

Complete Adapter Integration


Sources: src/Adapter/HyperfLogFactory.php1-19 src/Adapter/LogFactoryAdapter.php1-23 src/LogManager.php93-104