diff --git a/packages/legacy/app/package.json b/packages/legacy/app/package.json index ca0fb6f79d..668a4fa9c7 100644 --- a/packages/legacy/app/package.json +++ b/packages/legacy/app/package.json @@ -68,6 +68,7 @@ "react-native-gifted-chat": "*", "react-native-keychain": "^8.1.1", "react-native-localize": "^2.2.4", + "react-native-logs": "^5.1.0", "react-native-orientation-locker": "^1.6.0", "react-native-permissions": "^4.0.1", "react-native-qrcode-svg": "^6.2.0", diff --git a/packages/legacy/core/App/App.tsx b/packages/legacy/core/App/App.tsx index 9c8207bc5e..fc841bd5ab 100644 --- a/packages/legacy/core/App/App.tsx +++ b/packages/legacy/core/App/App.tsx @@ -16,7 +16,6 @@ import { proofRequestTourSteps } from './components/tour/ProofRequestTourSteps' import { Container, ContainerProvider } from './container-api' import { AnimatedComponentsProvider } from './contexts/animated-components' import { AuthProvider } from './contexts/auth' -import { CommonUtilProvider } from './contexts/commons' import { ConfigurationProvider } from './contexts/configuration' import { NetworkProvider } from './contexts/network' import { StoreProvider } from './contexts/store' @@ -49,31 +48,29 @@ function App(sytem: Container) { - - - - - - + + + + diff --git a/packages/legacy/core/App/container-api.ts b/packages/legacy/core/App/container-api.ts index 357ffa5d1b..74ea42b807 100644 --- a/packages/legacy/core/App/container-api.ts +++ b/packages/legacy/core/App/container-api.ts @@ -1,3 +1,4 @@ +import { BaseLogger } from '@credo-ts/core' import { StackNavigationProp } from '@react-navigation/stack' import React, { createContext, useContext } from 'react' import { DependencyContainer } from 'tsyringe' @@ -11,6 +12,7 @@ import { AuthenticateStackParams, ScreenOptionsType } from './types/navigators' export enum PROOF_TOKENS { GROUP_BY_REFERENT = 'proof.groupByReferant', } + export enum SCREEN_TOKENS { SCREEN_PREFACE = 'screen.preface', SCREEN_TERMS = 'screen.terms', @@ -41,6 +43,10 @@ export enum OBJECT_TOKENS { OBJECT_ONBOARDINGCONFIG = 'object.onboarding-config', } +export enum UTILITY_TOKENS { + UTIL_LOGGER = 'utility.logger', +} + export const TOKENS = { ...PROOF_TOKENS, ...SCREEN_TOKENS, @@ -50,6 +56,7 @@ export const TOKENS = { ...COMP_TOKENS, ...LOAD_STATE_TOKENS, ...OBJECT_TOKENS, + ...UTILITY_TOKENS, } export type FN_ONBOARDING_DONE = ( @@ -70,6 +77,7 @@ export interface TokenMapping { [TOKENS.LOAD_STATE]: FN_LOADSTATE [TOKENS.COMP_BUTTON]: Button [TOKENS.OBJECT_ONBOARDINGCONFIG]: ScreenOptionsType + [TOKENS.UTIL_LOGGER]: BaseLogger } export interface Container { diff --git a/packages/legacy/core/App/container-impl.ts b/packages/legacy/core/App/container-impl.ts index da7590f2ce..7b91abc646 100644 --- a/packages/legacy/core/App/container-impl.ts +++ b/packages/legacy/core/App/container-impl.ts @@ -15,6 +15,7 @@ import Onboarding from './screens/Onboarding' import Preface from './screens/Preface' import ScreenTerms, { TermsVersion } from './screens/Terms' import { loadLoginAttempt } from './services/keychain' +import { ConsoleLogger } from './services/logger' import { AuthenticateStackParams, Screens } from './types/navigators' import { Migration as MigrationState, @@ -23,7 +24,6 @@ import { Onboarding as StoreOnboardingState, Tours as ToursState, } from './types/state' - export class MainContainer implements Container { public static readonly TOKENS = TOKENS private container: DependencyContainer @@ -39,8 +39,9 @@ export class MainContainer implements Container { this.container.registerInstance(TOKENS.SCREEN_ONBOARDING, Onboarding) this.container.registerInstance(TOKENS.STACK_ONBOARDING, OnboardingStack) this.container.registerInstance(TOKENS.COMP_BUTTON, Button) - this.container.registerInstance(TOKENS.OBJECT_ONBOARDINGCONFIG, DefaultScreenOptionsDictionary) this.container.registerInstance(TOKENS.GROUP_BY_REFERENT, false) + this.container.registerInstance(TOKENS.OBJECT_ONBOARDINGCONFIG, DefaultScreenOptionsDictionary) + this.container.registerInstance(TOKENS.UTIL_LOGGER, new ConsoleLogger()) this.container.registerInstance( TOKENS.FN_ONBOARDING_DONE, diff --git a/packages/legacy/core/App/contexts/commons.tsx b/packages/legacy/core/App/contexts/commons.tsx deleted file mode 100644 index e67753c4cb..0000000000 --- a/packages/legacy/core/App/contexts/commons.tsx +++ /dev/null @@ -1,35 +0,0 @@ -import React, { createContext, useContext } from 'react' - -import { AppConsoleLogger, LogLevel } from '../services/logger' - -export interface AppCommonsContext { - log: (message: string, logLevel: Exclude) => void -} - -export const AppCommons = createContext(null as unknown as AppCommonsContext) - -export const CommonUtilProvider: React.FC = ({ children }) => { - const logger = new AppConsoleLogger(LogLevel.test) - const log = async (messaage: string, logLevel: Exclude) => { - logger.log(logLevel, messaage) - //TODO: do some more logic like collecting errors for later logging and investigation - } - - return ( - - {children} - - ) -} - -export const useCommons = () => { - const commonsContext = useContext(AppCommons) - if (!commonsContext) { - throw new Error('commonsContext must be used within a CommonUtilProvider') - } - return commonsContext -} diff --git a/packages/legacy/core/App/index.ts b/packages/legacy/core/App/index.ts index 5882fd76e8..888a627742 100644 --- a/packages/legacy/core/App/index.ts +++ b/packages/legacy/core/App/index.ts @@ -30,7 +30,6 @@ import HomeFooterView from './components/views/HomeFooterView' import indyLedgers from './configs/ledgers/indy' import * as contexts from './contexts' import { AuthProvider } from './contexts/auth' -import { CommonUtilProvider } from './contexts/commons' import { NetworkProvider } from './contexts/network' import { useTour } from './contexts/tour/tour-context' import { TourProvider } from './contexts/tour/tour-provider' @@ -107,7 +106,6 @@ export { App, indyLedgers, Agent, - CommonUtilProvider, AgentProvider, AuthProvider, NetworkProvider, diff --git a/packages/legacy/core/App/screens/Splash.tsx b/packages/legacy/core/App/screens/Splash.tsx index a59c143637..c729a8f09d 100644 --- a/packages/legacy/core/App/screens/Splash.tsx +++ b/packages/legacy/core/App/screens/Splash.tsx @@ -1,4 +1,4 @@ -import { Agent, ConsoleLogger, HttpOutboundTransport, LogLevel, WsOutboundTransport } from '@credo-ts/core' +import { Agent, HttpOutboundTransport, WsOutboundTransport } from '@credo-ts/core' import { useAgent } from '@credo-ts/react-hooks' import { agentDependencies } from '@credo-ts/react-native' import { useNavigation } from '@react-navigation/core' @@ -96,6 +96,7 @@ const Splash: React.FC = () => { const container = useContainer() const [mounted, setMounted] = useState(false) const { version: TermsVersion } = container.resolve(TOKENS.SCREEN_TERMS) + const logger = container.resolve(TOKENS.UTIL_LOGGER) const styles = StyleSheet.create({ container: { flex: 1, @@ -220,7 +221,7 @@ const Splash: React.FC = () => { id: credentials.id, key: credentials.key, }, - logger: new ConsoleLogger(LogLevel.trace), + logger, autoUpdateStorageOnStartup: true, }, dependencies: agentDependencies, diff --git a/packages/legacy/core/App/services/logger.ts b/packages/legacy/core/App/services/logger.ts index 0adcf678d5..9ecbb36074 100644 --- a/packages/legacy/core/App/services/logger.ts +++ b/packages/legacy/core/App/services/logger.ts @@ -1,86 +1,62 @@ -/* eslint-disable no-console,@typescript-eslint/no-explicit-any */ - -export enum LogLevel { - test = 0, - trace = 1, - debug = 2, - info = 3, - warn = 4, - error = 5, - fatal = 6, - off = 7, -} +import { BaseLogger } from '@credo-ts/core' +import { consoleTransport, logger } from 'react-native-logs' + +export class ConsoleLogger extends BaseLogger { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + private log: any + private config = { + levels: { + test: 0, + trace: 0, + debug: 0, + info: 1, + warn: 2, + error: 3, + fatal: 4, + }, + severity: 'debug', + async: true, + dateFormat: 'time', + printDate: false, + } -export interface Logger { - logLevel: LogLevel + public constructor() { + super() - test(message: string, data?: Record): void - trace(message: string, data?: Record): void - debug(message: string, data?: Record): void - info(message: string, data?: Record): void - warn(message: string, data?: Record): void - error(message: string, data?: Record): void - fatal(message: string, data?: Record): void -} + const transport = [consoleTransport] + const config = { + ...this.config, + transport, + } -const replaceError = (_: unknown, value: unknown) => { - if (value instanceof Error) { - const newValue = Object.getOwnPropertyNames(value).reduce( - (obj, propName) => { - obj[propName] = (value as unknown as Record)[propName] - return obj - }, - { name: value.name } as Record - ) - return newValue + this.log = logger.createLogger<'test' | 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'>(config) } - return value -} - -export class AppConsoleLogger { - public logLevel: LogLevel - - // Map our log levels to console levels - private consoleLogMap = { - [LogLevel.test]: 'log', - [LogLevel.trace]: 'log', - [LogLevel.debug]: 'debug', - [LogLevel.info]: 'info', - [LogLevel.warn]: 'warn', - [LogLevel.error]: 'error', - [LogLevel.fatal]: 'error', - } as const + public test(message: string, data?: object | undefined): void { + this.log?.test(message, data) + } - public constructor(logLevel: LogLevel = LogLevel.off) { - this.logLevel = logLevel + public trace(message: string, data?: object | undefined): void { + this.log?.trace(message, data) } - public log(level: Exclude, message: string, data?: Record): void { - // Get console method from mapping - const consoleLevel = this.consoleLogMap[level] + public debug(message: string, data?: object | undefined): void { + this.log?.debug(message, data) + } - // Get logger prefix from log level names in enum - const prefix = LogLevel[level].toUpperCase() + public info(message: string, data?: object | undefined): void { + this.log?.info(message, data) + } - // Return early if logging is not enabled for this level - if (!this.isEnabled(level)) { - console.log('logger disabled') - return - } + public warn(message: string, data?: object | undefined): void { + this.log?.warn(message, data) + } - // Log, with or without data - if (data) { - console[consoleLevel]( - `${prefix}: ${new Date().toISOString()} - ${message}`, - JSON.stringify(data, replaceError, 2) - ) - } else { - console[consoleLevel](`${prefix}: ${new Date().toISOString()} - ${message}`) - } + public error(message: string, data?: object | undefined): void { + this.log?.error(message, data) } - private isEnabled(logLevel: LogLevel) { - return logLevel >= this.logLevel + public fatal(message: string, data?: object | undefined): void { + this.log?.fatal(message, data) } } diff --git a/packages/legacy/core/package.json b/packages/legacy/core/package.json index 8670a9b406..c317d86204 100644 --- a/packages/legacy/core/package.json +++ b/packages/legacy/core/package.json @@ -198,6 +198,7 @@ "react-native-gifted-chat": "*", "react-native-keychain": "^8.1.1", "react-native-localize": "^2.2.4", + "react-native-logs": "^5.1.0", "react-native-orientation-locker": "*", "react-native-permissions": "^4.0.1", "react-native-qrcode-svg": "^6.2.0", diff --git a/packages/remote-logs/package.json b/packages/remote-logs/package.json index 031bb1d1f2..302c607638 100644 --- a/packages/remote-logs/package.json +++ b/packages/remote-logs/package.json @@ -40,7 +40,7 @@ "eslint-import-resolver-typescript": "^2.5.0", "react": "18.2.0", "react-native": "0.72.5", - "react-native-logs": "^5.0.1", + "react-native-logs": "^5.1.0", "rimraf": "^5.0.0", "typescript": "^5.0.4" }, @@ -50,6 +50,6 @@ "buffer": "^6.0.3", "react": "^18.2.0", "react-native": "^0.72.5", - "react-native-logs": "^5.0.1" + "react-native-logs": "^5.1.0" } -} +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index f3763bbe18..b2b800256d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4623,6 +4623,7 @@ __metadata: react-native-gifted-chat: "*" react-native-keychain: ^8.1.1 react-native-localize: ^2.2.4 + react-native-logs: ^5.1.0 react-native-orientation-locker: "*" react-native-permissions: ^4.0.1 react-native-qrcode-svg: ^6.2.0 @@ -4656,7 +4657,7 @@ __metadata: eslint-import-resolver-typescript: ^2.5.0 react: 18.2.0 react-native: 0.72.5 - react-native-logs: ^5.0.1 + react-native-logs: ^5.1.0 rimraf: ^5.0.0 typescript: ^5.0.4 peerDependencies: @@ -4665,7 +4666,7 @@ __metadata: buffer: ^6.0.3 react: ^18.2.0 react-native: ^0.72.5 - react-native-logs: ^5.0.1 + react-native-logs: ^5.1.0 languageName: unknown linkType: soft @@ -8224,6 +8225,7 @@ __metadata: react-native-gifted-chat: "*" react-native-keychain: ^8.1.1 react-native-localize: ^2.2.4 + react-native-logs: ^5.1.0 react-native-orientation-locker: ^1.6.0 react-native-permissions: ^4.0.1 react-native-qrcode-svg: ^6.2.0 @@ -19896,10 +19898,10 @@ __metadata: languageName: node linkType: hard -"react-native-logs@npm:^5.0.1": - version: 5.0.1 - resolution: "react-native-logs@npm:5.0.1" - checksum: 71af385c85a7d45d2046ee9fa05ec49e989a446660742d732e2ba210528fb5145f81308af3f65be607bbf207a875480344cca3ec5e7943a7c10ba2d882f28266 +"react-native-logs@npm:^5.1.0": + version: 5.1.0 + resolution: "react-native-logs@npm:5.1.0" + checksum: 21a26b0b2d60facbc0207c0e402cb351e3d4ffb41c44297c0a1af6968130e06556ae302153ae6bd8707d79046efc85a277b93cff26dbdfebd67a63a0323e76ed languageName: node linkType: hard