|
| 1 | +--- |
| 2 | +sidebar_position: 2.1 |
| 3 | +--- |
| 4 | + |
| 5 | +# Configuration |
| 6 | + |
| 7 | +Starknet.js has behaviors that can be adjusted through its configurations: `config` and `logger`. |
| 8 | + |
| 9 | +## Config |
| 10 | + |
| 11 | +The core global configuration is a singleton object containing case-sensitive global default properties. |
| 12 | +Each property can be configured before the rest of the code is run to modify their corresponding behavior. |
| 13 | +When they overlap, constructor and method parameters have higher precedence over the global configuration defaults. |
| 14 | +Custom keys can also be used to store and use arbitrary values during runtime. |
| 15 | + |
| 16 | +```ts |
| 17 | +import { config } from 'starknet'; |
| 18 | + |
| 19 | +// Set existing or custom global property |
| 20 | +config.set('mode', 'DEFAULT'); |
| 21 | + |
| 22 | +// Retrieve entire configuration |
| 23 | +config.getAll(); |
| 24 | + |
| 25 | +// Retrieve single global property |
| 26 | +config.get('legacyMode'); |
| 27 | + |
| 28 | +// Update (merge) existing configuration with modified or custom property |
| 29 | +config.update({ logLevel: 'DEBUG', newKey: 'value' }); |
| 30 | + |
| 31 | +// Reset config to initial global configuration |
| 32 | +config.reset(); |
| 33 | + |
| 34 | +// Delete existing global property |
| 35 | +config.delete('newKey'); |
| 36 | + |
| 37 | +// Check existence of the global property |
| 38 | +config.hasKey('newKey'); |
| 39 | +``` |
| 40 | + |
| 41 | +### Global parameters and Default Global Configuration |
| 42 | + |
| 43 | +Default global configuration is the initial state that global configuration starts with. |
| 44 | + |
| 45 | +Details can be found in [global/constants.ts](https://github.com/starknet-io/starknet.js/blob/develop/src/global/constants.ts) |
| 46 | + |
| 47 | +```ts |
| 48 | + logLevel: 'INFO', // verbosity levels of the system logger, more details under logger |
| 49 | + accountTxVersion: ETransactionVersion.V2, // by default use V2 transactions in Account class instances |
| 50 | + legacyMode: false, // enable legacy transaction types (note: this could break the code depending on the Starknet version used by the network) |
| 51 | +``` |
| 52 | + |
| 53 | +## Logger |
| 54 | + |
| 55 | +Logger is a singleton object through which the Starknet.js logs are managed. |
| 56 | + |
| 57 | +Supported log levels: |
| 58 | + |
| 59 | +| | | | |
| 60 | +| :-----: | --- | ----------------------------- | |
| 61 | +| `DEBUG` | 5 | show all logs | |
| 62 | +| `INFO` | 4 | show INFO, WARN, ERROR, FATAL | |
| 63 | +| `WARN` | 3 | show WARN, ERROR, FATAL | |
| 64 | +| `ERROR` | 2 | show ERROR, FATAL | |
| 65 | +| `FATAL` | 1 | show only FATAL | |
| 66 | +| `OFF` | 0 | disable logs | |
| 67 | + |
| 68 | +```ts |
| 69 | +import { logger } from 'starknet'; |
| 70 | + |
| 71 | +// set custom log level (can also be done using global config) |
| 72 | +logger.setLogLevel('WARN'); |
| 73 | + |
| 74 | +// get current log level |
| 75 | +logger.getLogLevel(); |
| 76 | + |
| 77 | +// get a list of all verbosity modes that would be displayed with the current log level |
| 78 | +logger.getEnabledLogLevels(); |
| 79 | +``` |
| 80 | + |
| 81 | +Developers can also use it to add custom logs. |
| 82 | + |
| 83 | +```ts |
| 84 | +import { logger } from 'starknet'; |
| 85 | + |
| 86 | +logger.debug('Debug message', additionalDataObject); |
| 87 | +logger.info('Info message', additionalDataObject); |
| 88 | +logger.warn('Warn message', additionalDataObject); |
| 89 | +logger.error('Error message', additionalDataObject); |
| 90 | +logger.fatal('Fatal message', additionalDataObject); |
| 91 | +``` |
0 commit comments