# phpman > perldoc > Log::Any::Adapter

## NAME
    [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown) - Tell [Log::Any](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny/markdown) where to send its logs

## VERSION
    version 1.710

## SYNOPSIS
        # Log to a file, or stdout, or stderr for all categories
        #
        use [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown) ('File', '/path/to/file.log');
        use [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown) ('Stdout');
        use [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown) ('Stderr');

        # Use [Log::Log4perl](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3ALog4perl/markdown) for all categories
        #
        [Log::Log4perl::init](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3ALog4perl%3A%3Ainit/markdown)('/etc/log4perl.conf');
        [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set('Log4perl');

        # Use [Log::Dispatch](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3ADispatch/markdown) for [Foo::Baz](https://www.chedong.com/phpMan.php/perldoc/Foo%3A%3ABaz/markdown)
        #
        use [Log::Dispatch](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3ADispatch/markdown);
        my $log = [Log::Dispatch](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3ADispatch/markdown)->new(outputs => [[ ... ]]);
        [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set( { category => '[Foo::Baz](https://www.chedong.com/phpMan.php/perldoc/Foo%3A%3ABaz/markdown)' },
            'Dispatch', dispatcher => $log );

        # Use [Log::Dispatch::Config](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3ADispatch%3A%3AConfig/markdown) for [Foo::Baz](https://www.chedong.com/phpMan.php/perldoc/Foo%3A%3ABaz/markdown) and its subcategories
        #
        use [Log::Dispatch::Config](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3ADispatch%3A%3AConfig/markdown);
        [Log::Dispatch::Config](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3ADispatch%3A%3AConfig/markdown)->configure('/path/to/log.conf');
        [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set(
            { category => qr/^[Foo::Baz](https://www.chedong.com/phpMan.php/perldoc/Foo%3A%3ABaz/markdown)/ },
            'Dispatch', dispatcher => [Log::Dispatch::Config](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3ADispatch%3A%3AConfig/markdown)->instance() );

        # Use your own adapter for all categories
        #
        [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set('+[My::Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/My%3A%3ALog%3A%3AAny%3A%3AAdapter/markdown)', ...);

## DESCRIPTION
    [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown) connects log producers and log consumers. Its methods instantiate a logging
    adapter (a subclass of [Log::Any::Adapter::Base](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter%3A%3ABase/markdown)) and route log messages from one or more
    categories to it.

## ADAPTERS
    In order to use a logging mechanism with "[Log::Any](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny/markdown)", there needs to be an adapter class for it.
    Typically this is named [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)::*something*.

### Adapters in this distribution
    Three basic adapters come with this distribution -- [Log::Any::Adapter::File](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter%3A%3AFile/markdown),
    [Log::Any::Adapter::Stdout](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter%3A%3AStdout/markdown) and [Log::Any::Adapter::Stderr](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter%3A%3AStderr/markdown):

        use [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown) ('File', '/path/to/file.log');
        use [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown) ('Stdout');
        use [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown) ('Stderr');

        # or

        use [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown);
        [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set('File', '/path/to/file.log');
        [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set('Stdout');
        [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set('Stderr');

    All of them simply output the message and newline to the specified destination; a datestamp
    prefix is added in the "File" case. For anything more complex you'll want to use a more robust
    adapter from CPAN.

### Adapters on CPAN
    A sampling of adapters available on CPAN as of this writing:

    *   [Log::Any::Adapter::Log4perl](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter%3A%3ALog4perl/markdown)

    *   [Log::Any::Adapter::Dispatch](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter%3A%3ADispatch/markdown)

    *   [Log::Any::Adapter::FileHandle](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter%3A%3AFileHandle/markdown)

    *   [Log::Any::Adapter::Syslog](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter%3A%3ASyslog/markdown)

    You may find other adapters on CPAN by searching for "[Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)", or create your own
    adapter. See [Log::Any::Adapter::Development](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter%3A%3ADevelopment/markdown) for more information on the latter.

## SETTING AND REMOVING ADAPTERS
    [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set ([options, ]adapter_name, adapter_params...)
        This method sets the adapter to use for all log categories, or for a particular set of
        categories.

        *adapter_name* is the name of an adapter. It is automatically prepended with
        "[Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)::". If instead you want to pass the full name of an adapter, prefix it
        with a "+". e.g.

            # Use [My::Adapter](https://www.chedong.com/phpMan.php/perldoc/My%3A%3AAdapter/markdown) class
            [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set('+[My::Adapter](https://www.chedong.com/phpMan.php/perldoc/My%3A%3AAdapter/markdown)', arg => $value);

        *adapter_params* are passed along to the adapter constructor. See the documentation for the
        individual adapter classes for more information.

        An optional hash of *options* may be passed as the first argument. Options are:

        category
            A string containing a category name, or a regex (created with "qr//") matching multiple
            categories. If not specified, all categories will be routed to the adapter.

        lexically
            A reference to a lexical variable. When the variable goes out of scope, the adapter
            setting will be removed. e.g.

                {
                    [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set({lexically => \my $lex}, ...);

                    # in effect here
                    ...
                }
                # no longer in effect here

        "set" returns an entry object, which can be passed to "remove". If you call "set" repeatedly
        without calling "remove" you will leak memory. For most programs that set an adapter once
        until the end of the program, this shouldn't matter.

    use [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown) (...)
        If you pass arguments to "use [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)", it calls "[Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set" with
        those arguments.

    [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->remove (entry)
        Remove an *entry* previously returned by "set".

## USING MORE THAN ONE ADAPTER
    "[Log::Any](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny/markdown)" maintains a stack of entries created via "set". If you call "set" repeatedly, you
    will leak memory unless you do one of the following:

    When getting a logger for a particular category, "[Log::Any](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny/markdown)" will work its way down the stack and
    use the first matching entry.

    Whenever the stack changes, any "[Log::Any](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny/markdown)" loggers that have previously been created will
    automatically adjust to the new stack. For example:

        my $log = [Log::Any](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny/markdown)->get_logger();
        $log->error("aiggh!");   # this goes nowhere
        ...
        {
            [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->set({ lexically => \my $lex }, 'Log4perl');
            $log->error("aiggh!");   # this goes to log4perl
            ...
        }
        $log->error("aiggh!");   # this goes nowhere again

BUILDING ON THE [Log::Any](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny/markdown) BACKEND
    get
          my $adapter= [Log::Any::Adapter](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter/markdown)->get($category);

        The primary intended way to extend the producing-side of [Log::Any](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny/markdown) is with a custom
        [Log::Any::Proxy](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AProxy/markdown) class. However, for special logging scenarios you might also just want
        access to the adapter for a given category. The API of an adapter object is described in
        [Log::Any::Adapter::Development](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny%3A%3AAdapter%3A%3ADevelopment/markdown). Beware that adapter objects can be "rewritten" on the fly,
        so any conditional behavior you write depending on the capabilities of an adapter must be
        re-checked every time you access the adapter.

## SEE ALSO
    [Log::Any](https://www.chedong.com/phpMan.php/perldoc/Log%3A%3AAny/markdown)

## AUTHORS
    *   Jonathan Swartz <<swartz@pobox.com>>

    *   David Golden <<dagolden@cpan.org>>

    *   Doug Bell <<preaction@cpan.org>>

    *   Daniel Pittman <<daniel@rimspace.net>>

    *   Stephen Thirlwall <<sdt@cpan.org>>

## COPYRIGHT AND LICENSE
    This software is copyright (c) 2017 by Jonathan Swartz, David Golden, and Doug Bell.

    This is free software; you can redistribute it and/or modify it under the same terms as the Perl
    5 programming language system itself.

