Skip to main content
Version: Next

ServiceLocator

Service locator for managing the services used by Crawlee.

All services are initialized to their default value lazily.

There are two primary usage patterns:

1. Global service locator (for default services):

import { serviceLocator, BasicCrawler } from 'crawlee';

// Optionally configure global services before creating crawlers
serviceLocator.setStorageBackend(myCustomClient);

// Crawler uses global services
const crawler = new BasicCrawler({ ... });

2. Per-crawler services (recommended for isolation):

import { BasicCrawler, Configuration, LocalEventManager, MemoryStorageBackend } from 'crawlee';

const crawler = new BasicCrawler({
requestHandler: async ({ request }) => { ... },
configuration: new Configuration({ ... }), // custom configuration
storageBackend: new MemoryStorageBackend(), // custom storage
eventManager: LocalEventManager.fromConfiguration(), // custom events
});
// Crawler has its own isolated ServiceLocator instance

Implements

  • ServiceLocatorInterface

Index

Constructors

constructor

  • new ServiceLocator(configuration, eventManager, storageBackend, logger): ServiceLocator
  • Creates a new ServiceLocator instance.


    Parameters

    • optionalconfiguration: Configuration

      Optional configuration instance to use

    • optionaleventManager: EventManager

      Optional event manager instance to use

    • optionalstorageBackend: StorageBackend

      Optional storage backend instance to use

    • optionallogger: CrawleeLogger

      Optional logger instance to use

    Returns ServiceLocator

Methods

getChildLog

  • Get a child logger with the given prefix. Equivalent to getLogger().child({ prefix }).


    Parameters

    • prefix: string

    Returns CrawleeLogger

getConfiguration

  • Get the configuration. Creates a default Configuration instance if none has been set.


    Returns Configuration

getEventManager

  • Get the event manager. Creates a default LocalEventManager instance if none has been set.


    Returns EventManager

getLogger

  • Get the logger. Returns the default @apify/log logger if none has been set.


    Returns CrawleeLogger

getStorageBackend

  • Get the storage backend. Creates a default storage backend if none has been set — FileSystemStorageBackend when persistStorage is enabled (the default), MemoryStorageBackend otherwise.


    Returns StorageBackend

getStorageInstanceManager

  • Get the storage instance manager (shared across all storage types).


    Returns StorageInstanceManager

setConfiguration

  • setConfiguration(configuration): void
  • Set the configuration.


    Parameters

    Returns void

    Throws - If a different configuration has already been retrieved

setEventManager

  • setEventManager(eventManager): void
  • Set the event manager.


    Parameters

    Returns void

    Throws - If a different event manager has already been retrieved

setLogger

  • setLogger(logger): void
  • Set the logger.


    Parameters

    Returns void

    Throws - If a different logger has already been retrieved

setStorageBackend

  • setStorageBackend(storageBackend): void
  • Set the storage backend.


    Parameters

    Returns void

    Throws - If a different storage backend has already been retrieved