Skip to content

Commit 589d404

Browse files
SteveGibsonCLbfopslcodesSteve BoytsunRReverser
authored
Logging API (#132)
## Description of Changes *Describe what has been changed, any new features or bug fixes* Changed logging based on [this proposal](clockworklabs/SpacetimeDBPrivate#981) ## API - [x] This is an API breaking change to the SDK *If the API is breaking, please state below what will break* Logging interface is different now. `Logger` has been renamed to `Log`, and its methods have been renamed as well (ex. `Logger.LogError` is now `Log.Error`) ## Requires SpacetimeDB PRs *List any PRs here that are required for this SDK change to work* --------- Co-authored-by: Zeke Foppa <[email protected]> Co-authored-by: Zeke Foppa <[email protected]> Co-authored-by: Jeremie Pelletier <[email protected]> Co-authored-by: Steve Boytsun <[email protected]> Co-authored-by: Ingvar Stepanyan <[email protected]>
1 parent 6312c45 commit 589d404

File tree

11 files changed

+183
-64
lines changed

11 files changed

+183
-64
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Git tree checks
2+
3+
on:
4+
pull_request:
5+
types: [opened, edited, reopened, synchronize]
6+
merge_group:
7+
permissions: read-all
8+
9+
jobs:
10+
check_base_ref:
11+
name: Only release branches may merge into master
12+
runs-on: ubuntu-latest
13+
steps:
14+
- id: not_based_on_master
15+
if: |
16+
github.event_name == 'pull_request' &&
17+
github.event.pull_request.base.ref == 'master' &&
18+
! startsWith(github.event.pull_request.head.ref, 'release/')
19+
run: |
20+
echo 'Only `release/*` branches are allowed to merge into `master`.'
21+
echo 'Maybe your PR should be merging into `staging`?'
22+
exit 1

sdks/csharp/DEVELOP.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
## `SpacetimeDB.ClientApi`
44

5-
To regenerate this namespace, run the `tools/gen-client-api.sh` or the
6-
`tools/gen-client-api.bat` script.
5+
To regenerate this namespace, run the `tools~/gen-client-api.sh` or the
6+
`tools~/gen-client-api.bat` script.

sdks/csharp/src/ClientCache.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public bool DeleteEntry(byte[] rowBytes)
4444
return true;
4545
}
4646

47-
Logger.LogWarning("Deleting value that we don't have (no cached value available)");
47+
Log.Warn("Deleting value that we don't have (no cached value available)");
4848
return false;
4949
}
5050

@@ -65,7 +65,7 @@ public void AddTable<T>()
6565

6666
if (!tables.TryAdd(name, new TableCache<T>()))
6767
{
68-
Logger.LogError($"Table with name already exists: {name}");
68+
Log.Error($"Table with name already exists: {name}");
6969
}
7070
}
7171

@@ -76,7 +76,7 @@ public void AddTable<T>()
7676
return table;
7777
}
7878

79-
Logger.LogError($"We don't know that this table is: {name}");
79+
Log.Error($"We don't know that this table is: {name}");
8080
return null;
8181
}
8282

sdks/csharp/src/ConsoleLogger.cs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ public enum LogLevel
99
{
1010
None = 0,
1111
Debug = 1,
12-
Warning = 2,
13-
Error = 4,
14-
Exception = 8,
15-
All = Debug | Warning | Error | Exception
12+
Trace = 2,
13+
Info = 4,
14+
Warning = 8,
15+
Error = 16,
16+
Exception = 32,
17+
All = ~0
1618
}
1719
LogLevel _logLevel;
1820

@@ -21,35 +23,59 @@ public ConsoleLogger(LogLevel logLevel = LogLevel.All)
2123
_logLevel = logLevel;
2224
}
2325

24-
public void Log(string message)
26+
public void Debug(string message)
2527
{
2628
if (_logLevel.HasFlag(LogLevel.Debug))
2729
{
28-
Console.WriteLine(message);
30+
Console.WriteLine($"[D] {message}");
2931
}
3032
}
3133

32-
public void LogError(string message)
34+
public void Trace(string message)
35+
{
36+
if (_logLevel.HasFlag(LogLevel.Trace))
37+
{
38+
Console.WriteLine($"[T] {message}");
39+
}
40+
}
41+
42+
public void Info(string message)
43+
{
44+
if (_logLevel.HasFlag(LogLevel.Info))
45+
{
46+
Console.WriteLine($"[I] {message}");
47+
}
48+
}
49+
50+
public void Warn(string message)
51+
{
52+
if (_logLevel.HasFlag(LogLevel.Warning))
53+
{
54+
Console.WriteLine($"[W] {message}");
55+
}
56+
}
57+
58+
public void Error(string message)
3359
{
3460
if (_logLevel.HasFlag(LogLevel.Error))
3561
{
36-
Console.WriteLine($"Error: {message}");
62+
Console.WriteLine($"[E] {message}");
3763
}
3864
}
3965

40-
public void LogException(Exception e)
66+
public void Exception(string message)
4167
{
4268
if (_logLevel.HasFlag(LogLevel.Exception))
4369
{
44-
Console.WriteLine($"Exception: {e.Message}");
70+
Console.WriteLine($"[X] {message}");
4571
}
4672
}
4773

48-
public void LogWarning(string message)
74+
public void Exception(Exception exception)
4975
{
50-
if (_logLevel.HasFlag(LogLevel.Warning))
76+
if (_logLevel.HasFlag(LogLevel.Exception))
5177
{
52-
Console.WriteLine($"Warning: {message}");
78+
Console.WriteLine($"[X] {exception}");
5379
}
5480
}
5581
}

sdks/csharp/src/ISpacetimeDBLogger.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ namespace SpacetimeDB
44
{
55
public interface ISpacetimeDBLogger
66
{
7-
void Log(string message);
8-
void LogError(string message);
9-
void LogWarning(string message);
10-
void LogException(Exception e);
7+
void Debug(string message);
8+
void Trace(string message);
9+
void Info(string message);
10+
void Warn(string message);
11+
void Error(string message);
12+
void Exception(string message);
13+
void Exception(Exception e);
1114
}
1215

13-
public static class Logger
16+
public static class Log
1417
{
1518
public static ISpacetimeDBLogger Current =
1619

@@ -20,9 +23,12 @@ public static class Logger
2023
new ConsoleLogger();
2124
#endif
2225

23-
public static void Log(string message) => Current.Log(message);
24-
public static void LogError(string message) => Current.LogError(message);
25-
public static void LogWarning(string message) => Current.LogWarning(message);
26-
public static void LogException(Exception e) => Current.LogException(e);
26+
public static void Debug(string message) => Current.Debug(message);
27+
public static void Trace(string message) => Current.Trace(message);
28+
public static void Info(string message) => Current.Info(message);
29+
public static void Warn(string message) => Current.Warn(message);
30+
public static void Error(string message) => Current.Error(message);
31+
public static void Exception(string message) => Current.Exception(message);
32+
public static void Exception(Exception exception) => Current.Exception(exception);
2733
}
2834
}

sdks/csharp/src/SpacetimeDBClient.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
207207
var table = clientDB.GetTable(tableName);
208208
if (table == null)
209209
{
210-
Logger.LogError($"Unknown table name: {tableName}");
210+
Log.Error($"Unknown table name: {tableName}");
211211
continue;
212212
}
213213

214214
if (update.Deletes.Count != 0)
215215
{
216-
Logger.LogWarning("Non-insert during a subscription update!");
216+
Log.Warn("Non-insert during a subscription update!");
217217
}
218218

219219
var hashSet = GetInsertHashSet(table.ClientTableType, initialSubscription.DatabaseUpdate.Tables.Count);
@@ -240,7 +240,7 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
240240
break;
241241

242242
case EncodedValue.Text(var txt):
243-
Logger.LogWarning("JavaScript messages are unsupported.");
243+
Log.Warn("JavaScript messages are unsupported.");
244244
break;
245245
}
246246
}
@@ -261,7 +261,7 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
261261
var table = clientDB.GetTable(tableName);
262262
if (table == null)
263263
{
264-
Logger.LogError($"Unknown table name: {tableName}");
264+
Log.Error($"Unknown table name: {tableName}");
265265
continue;
266266
}
267267

@@ -279,7 +279,7 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
279279
{
280280
if ((op.insert is not null && oldOp.insert is not null) || (op.delete is not null && oldOp.delete is not null))
281281
{
282-
Logger.LogWarning($"Update with the same primary key was applied multiple times! tableName={tableName}");
282+
Log.Warn($"Update with the same primary key was applied multiple times! tableName={tableName}");
283283
// TODO(jdetter): Is this a correctable error? This would be a major error on the
284284
// SpacetimeDB side.
285285
continue;
@@ -315,7 +315,7 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
315315
{
316316
if ((op.insert is not null && oldOp.insert is not null) || (op.delete is not null && oldOp.delete is not null))
317317
{
318-
Logger.LogWarning($"Update with the same primary key was applied multiple times! tableName={tableName}");
318+
Log.Warn($"Update with the same primary key was applied multiple times! tableName={tableName}");
319319
// TODO(jdetter): Is this a correctable error? This would be a major error on the
320320
// SpacetimeDB side.
321321
continue;
@@ -348,13 +348,13 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
348348
}
349349
catch (Exception e)
350350
{
351-
Logger.LogException(e);
351+
Log.Exception(e);
352352
}
353353
break;
354354
case UpdateStatus.Failed(var failed):
355355
break;
356356
case UpdateStatus.OutOfEnergy(var outOfEnergy):
357-
Logger.LogWarning("Failed to execute reducer: out of energy.");
357+
Log.Warn("Failed to execute reducer: out of energy.");
358358
break;
359359
default:
360360
throw new InvalidOperationException();
@@ -368,7 +368,7 @@ HashSet<byte[]> GetInsertHashSet(System.Type tableType, int tableSize)
368368

369369
if (!waitingOneOffQueries.Remove(messageId, out var resultSource))
370370
{
371-
Logger.LogError($"Response to unknown one-off-query: {messageId}");
371+
Log.Error($"Response to unknown one-off-query: {messageId}");
372372
break;
373373
}
374374

@@ -442,7 +442,7 @@ public void Connect(string? token, string uri, string addressOrName)
442442
uri = $"ws://{uri}";
443443
}
444444

445-
Logger.Log($"SpacetimeDBClient: Connecting to {uri} {addressOrName}");
445+
Log.Info($"SpacetimeDBClient: Connecting to {uri} {addressOrName}");
446446
Task.Run(async () =>
447447
{
448448
try
@@ -453,11 +453,11 @@ public void Connect(string? token, string uri, string addressOrName)
453453
{
454454
if (connectionClosed)
455455
{
456-
Logger.Log("Connection closed gracefully.");
456+
Log.Info("Connection closed gracefully.");
457457
return;
458458
}
459459

460-
Logger.LogException(e);
460+
Log.Exception(e);
461461
}
462462
});
463463
}
@@ -476,7 +476,7 @@ private void OnMessageProcessCompleteUpdate(ReducerEvent? dbEvent, List<DbOp> db
476476
}
477477
catch (Exception e)
478478
{
479-
Logger.LogException(e);
479+
Log.Exception(e);
480480
}
481481
}
482482
}
@@ -541,7 +541,7 @@ private void OnMessageProcessCompleteUpdate(ReducerEvent? dbEvent, List<DbOp> db
541541
}
542542
catch (Exception e)
543543
{
544-
Logger.LogException(e);
544+
Log.Exception(e);
545545
}
546546
}
547547
}
@@ -566,7 +566,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
566566
}
567567
catch (Exception e)
568568
{
569-
Logger.LogException(e);
569+
Log.Exception(e);
570570
}
571571
break;
572572
case ServerMessage.TransactionUpdate(var transactionUpdate):
@@ -581,7 +581,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
581581
var requestId = transactionUpdate.ReducerCall.RequestId;
582582
if (!stats.ReducerRequestTracker.FinishTrackingRequest(requestId))
583583
{
584-
Logger.LogWarning($"Failed to finish tracking reducer request: {requestId}");
584+
Log.Warn($"Failed to finish tracking reducer request: {requestId}");
585585
}
586586
}
587587
OnMessageProcessCompleteUpdate(processed.reducerEvent, dbOps);
@@ -591,7 +591,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
591591
}
592592
catch (Exception e)
593593
{
594-
Logger.LogException(e);
594+
Log.Exception(e);
595595
}
596596

597597
if (processed.reducerEvent is not { } reducerEvent)
@@ -607,7 +607,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
607607
}
608608
catch (Exception e)
609609
{
610-
Logger.LogException(e);
610+
Log.Exception(e);
611611
}
612612

613613
if (!reducerFound && transactionUpdate.Status is UpdateStatus.Failed(var failed))
@@ -618,7 +618,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
618618
}
619619
catch (Exception e)
620620
{
621-
Logger.LogException(e);
621+
Log.Exception(e);
622622
}
623623
}
624624
break;
@@ -631,7 +631,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
631631
}
632632
catch (Exception e)
633633
{
634-
Logger.LogException(e);
634+
Log.Exception(e);
635635
}
636636
break;
637637
case ServerMessage.OneOffQueryResponse:
@@ -641,7 +641,7 @@ private void OnMessageProcessComplete(PreProcessedMessage preProcessed)
641641
}
642642
catch (Exception e)
643643
{
644-
Logger.LogException(e);
644+
Log.Exception(e);
645645
}
646646

647647
break;
@@ -659,7 +659,7 @@ public void InternalCallReducer<T>(T args)
659659
{
660660
if (!webSocket.IsConnected)
661661
{
662-
Logger.LogError("Cannot call reducer, not connected to server!");
662+
Log.Error("Cannot call reducer, not connected to server!");
663663
return;
664664
}
665665

@@ -677,7 +677,7 @@ public void Subscribe(List<string> queries)
677677
{
678678
if (!webSocket.IsConnected)
679679
{
680-
Logger.LogError("Cannot subscribe, not connected to server!");
680+
Log.Error("Cannot subscribe, not connected to server!");
681681
return;
682682
}
683683

@@ -714,13 +714,13 @@ public async Task<T[]> OneOffQuery<T>(string query)
714714

715715
if (!stats.OneOffRequestTracker.FinishTrackingRequest(requestId))
716716
{
717-
Logger.LogWarning($"Failed to finish tracking one off request: {requestId}");
717+
Log.Warn($"Failed to finish tracking one off request: {requestId}");
718718
}
719719

720720
T[] LogAndThrow(string error)
721721
{
722722
error = $"While processing one-off-query `{queryString}`, ID {messageId}: {error}";
723-
Logger.LogError(error);
723+
Log.Error(error);
724724
throw new Exception(error);
725725
}
726726

0 commit comments

Comments
 (0)