@@ -68,7 +68,11 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
6868 /// null when disconnected, present when connecting or connected
6969 AbortController ? _disconnecter;
7070
71- /// The Logger internally used by this PowerSyncDatabase
71+ /// The Logger used by this [PowerSyncDatabase] .
72+ ///
73+ /// The default is [autoLogger] , which logs to the console in debug builds.
74+ /// Use [debugLogger] to always log to the console.
75+ /// Use [attachedLogger] to propagate logs to [Logger.root] for custom logging.
7276 late final Logger logger;
7377
7478 /// Open a [PowerSyncDatabase] .
@@ -83,17 +87,20 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
8387 /// from the last committed write transaction.
8488 ///
8589 /// A maximum of [maxReaders] concurrent read transactions are allowed.
90+ ///
91+ /// [logger] defaults to [autoLogger] , which logs to the console in debug builds.
8692 factory PowerSyncDatabase (
8793 {required Schema schema,
8894 required String path,
8995 int maxReaders = SqliteDatabase .defaultMaxReaders,
90- LogType log = LogType .auto ,
96+ Logger ? logger ,
9197 @Deprecated ("Use [PowerSyncDatabase.withFactory] instead" )
9298 // ignore: deprecated_member_use_from_same_package
9399 SqliteConnectionSetup ? sqliteSetup}) {
94100 // ignore: deprecated_member_use_from_same_package
95101 var factory = PowerSyncOpenFactory (path: path, sqliteSetup: sqliteSetup);
96- return PowerSyncDatabase .withFactory (factory , schema: schema, log: log);
102+ return PowerSyncDatabase .withFactory (factory ,
103+ schema: schema, logger: logger);
97104 }
98105
99106 /// Open a [PowerSyncDatabase] with a [PowerSyncOpenFactory] .
@@ -102,53 +109,33 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
102109 /// additional logic to run inside the database isolate before or after opening.
103110 ///
104111 /// Subclass [PowerSyncOpenFactory] to add custom logic to this process.
112+ ///
113+ /// [logger] defaults to [autoLogger] , which logs to the console in debug builds.
105114 factory PowerSyncDatabase .withFactory (
106115 PowerSyncOpenFactory openFactory, {
107116 required Schema schema,
108117 int maxReaders = SqliteDatabase .defaultMaxReaders,
109- LogType log = LogType .auto ,
118+ Logger ? logger ,
110119 }) {
111120 final db = SqliteDatabase .withFactory (openFactory, maxReaders: maxReaders);
112121 return PowerSyncDatabase .withDatabase (
113- schema: schema, database: db, log : log );
122+ schema: schema, database: db, logger : logger );
114123 }
115124
116125 /// Open a PowerSyncDatabase on an existing [SqliteDatabase] .
117126 ///
118127 /// Migrations are run on the database when this constructor is called.
128+ ///
129+ /// [logger] defaults to [autoLogger] , which logs to the console in debug builds.
119130 PowerSyncDatabase .withDatabase ({
120131 required this .schema,
121132 required this .database,
122- LogType log = LogType .auto ,
133+ Logger ? logger ,
123134 }) {
124- if (log == LogType .debug || log == LogType .auto) {
125- // Use a detached logger to log directly to the console
126- logger = Logger .detached ('PowerSync' );
127-
128- final debug = log == LogType .debug || kDebugMode;
129- if (debug) {
130- logger.level = Level .FINE ;
131- logger.onRecord.listen ((record) {
132- print (
133- '[${record .loggerName }] ${record .level .name }: ${record .time }: ${record .message }' );
134-
135- if (record.error != null ) {
136- print (record.error);
137- }
138- if (record.stackTrace != null ) {
139- print (record.stackTrace);
140- }
141- });
142- } else {
143- logger.level = Level .OFF ;
144- }
145- } else if (log == LogType .logger) {
146- // Standard logger. The app is responsible for adding an onRecord listener
147- // on the root logger.
148- logger = Logger ('PowerSync' );
135+ if (logger != null ) {
136+ this .logger = logger;
149137 } else {
150- // Should not happen
151- logger = Logger .detached ('PowerSync' );
138+ this .logger = autoLogger;
152139 }
153140
154141 updates = database.updates
@@ -643,15 +630,3 @@ Future<void> _powerSyncDatabaseIsolate(
643630 throw error;
644631 });
645632}
646-
647- enum LogType {
648- /// Log to the console, with FINE level in debug mode, no logs in release mode
649- auto,
650-
651- /// Always log to the console with FINE level
652- debug,
653-
654- /// Uses a Logger instance.
655- /// Use Logger.root.onRecord to handle log messages
656- logger,
657- }
0 commit comments