Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file logger RN example #1080

Merged
merged 1 commit into from
Sep 2, 2024
Merged

Add file logger RN example #1080

merged 1 commit into from
Sep 2, 2024

Conversation

dangeross
Copy link
Collaborator

This PR adds to the React Native example additional code to log to file SDK log entries and events, which can then be shared via email.

Added to the example are dependencies:

To configure the file logger, we disable capturing console logs by default and set the max number of log files to 5 (5 days of logs). You can additionally set the maximumFileSize to limit the file size:

FileLogger.configure({
    captureConsole: false,
    maximumNumberOfFiles: 5
})

We wrap the capturing of log entries to a utility class Log which can log both to console and optionally to file:

import { Log } from "../utils/logging"

const log = new Log("ExampleApp")
log.debug(`Log a debug log entry to file`, true)

We update the SDK logHandler in the HomeScreen to use the Log class to capture logs:

    const logHandler = (logEntry) => {
        switch (logEntry.level) {
            case "ERROR":
                log.error(`[ERROR]: ${logEntry.line}`, true)
                break
            case "TRACE":
                // Only log to file when in DEBUG mode
                log.debug(`[TRACE]: ${logEntry.line}`, DEBUG)
                break
            default:
                log.debug(`[${logEntry.level}]: ${logEntry.line}`, true)
                break
        }
    }

And finally add a button to trigger sending the log file via email:

        try {
            await FileLogger.sendLogFilesByEmail({
                subject: "Logs",
                body: `OS: ${Platform.OS} - ${Platform.Version}\n\n` + JSON.stringify(nodeInfo, null, 2)
            })
        } catch (e) {
            log.error(JSON.stringify(e))
        }

Copy link
Member

@roeierez roeierez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dangeross dangeross merged commit 1298055 into main Sep 2, 2024
8 of 9 checks passed
@dangeross dangeross deleted the savage-rn-log-file-example branch September 2, 2024 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants