• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nogipx / rpc_dart / 15778041469

20 Jun 2025 11:34AM UTC coverage: 76.097%. Remained the same
15778041469

push

github

web-flow
Create FUNDING.yml

2792 of 3669 relevant lines covered (76.1%)

6.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.67
/lib/src/logs/rpc_logger_extensions.dart
1
// SPDX-FileCopyrightText: 2025 Karim "nogipx" Mamatkazin <nogipx@gmail.com>
2
//
3
// SPDX-License-Identifier: LGPL-3.0-or-later
4

5
part of '_logs.dart';
6

7
/// Расширения для RpcLogger для удобного логирования RPC операций
8
extension RpcLoggerExtensions on RpcLogger {
9
  // ============================================================================
10
  // Stream операции
11
  // ============================================================================
12

13
  /// Логирует создание stream процессора
14
  Future<void> logStreamCreated({
×
15
    required String methodPath,
16
    required int streamId,
17
    String? processorType,
18
    RpcContext? context,
19
    Map<String, dynamic>? metadata,
20
  }) async {
21
    await internal(
×
22
      'Stream processor created',
23
      context: '$processorType:$methodPath',
×
24
      rpcContext: context,
25
      data: {
×
26
        'method_path': methodPath,
×
27
        'stream_id': streamId,
×
28
        'processor_type': processorType,
×
29
        if (metadata != null) ...metadata,
×
30
      },
31
    );
32
  }
33

34
  /// Логирует привязку к потоку сообщений
35
  Future<void> logStreamBound({
8✔
36
    required String methodPath,
37
    required int streamId,
38
    RpcContext? context,
39
  }) async {
40
    await internal(
8✔
41
      'Stream bound to message flow',
42
      context: 'binding:$methodPath',
8✔
43
      rpcContext: context,
44
      data: {
8✔
45
        'method_path': methodPath,
46
        'stream_id': streamId,
47
        'action': 'bind_to_stream',
48
      },
49
    );
50
  }
51

52
  /// Логирует обработку входящего сообщения
53
  Future<void> logMessageReceived({
8✔
54
    required int streamId,
55
    required String messageType,
56
    int? payloadSize,
57
    bool? isDirectPayload,
58
    RpcContext? context,
59
  }) async {
60
    await internal(
8✔
61
      'Message received',
62
      context: 'message:$messageType',
8✔
63
      rpcContext: context,
64
      data: {
8✔
65
        'stream_id': streamId,
66
        'message_type': messageType,
67
        'payload_size': payloadSize,
68
        'is_direct_payload': isDirectPayload,
69
        'action': 'message_received',
70
      },
71
    );
72
  }
73

74
  /// Логирует отправку ответа
75
  Future<void> logResponseSent({
×
76
    required int streamId,
77
    required String methodPath,
78
    int? responseSize,
79
    int? responseNumber,
80
    RpcContext? context,
81
  }) async {
82
    await internal(
×
83
      'Response sent',
84
      context: 'response:$methodPath',
×
85
      rpcContext: context,
86
      data: {
×
87
        'stream_id': streamId,
88
        'method_path': methodPath,
89
        'response_size': responseSize,
90
        'response_number': responseNumber,
91
        'action': 'response_sent',
92
      },
93
    );
94
  }
95

96
  /// Логирует завершение stream
97
  Future<void> logStreamFinished({
8✔
98
    required String methodPath,
99
    required int streamId,
100
    String? reason,
101
    RpcContext? context,
102
  }) async {
103
    await internal(
8✔
104
      'Stream finished',
105
      context: 'finish:$methodPath',
8✔
106
      rpcContext: context,
107
      data: {
8✔
108
        'method_path': methodPath,
109
        'stream_id': streamId,
110
        'reason': reason,
111
        'action': 'stream_finished',
112
      },
113
    );
114
  }
115

116
  // ============================================================================
117
  // Error handling
118
  // ============================================================================
119

120
  /// Логирует ошибку с контекстом RPC операции
121
  Future<void> logRpcError({
1✔
122
    required String operation,
123
    required Object error,
124
    StackTrace? stackTrace,
125
    String? methodPath,
126
    int? streamId,
127
    RpcContext? context,
128
    Map<String, dynamic>? metadata,
129
  }) async {
130
    await this.error(
1✔
131
      'RPC operation failed: $operation',
1✔
132
      context: 'error:${methodPath ?? 'unknown'}',
1✔
133
      error: error,
134
      stackTrace: stackTrace,
135
      rpcContext: context,
136
      data: {
1✔
137
        'operation': operation,
1✔
138
        'method_path': methodPath,
1✔
139
        'stream_id': streamId,
1✔
140
        'error_type': error.runtimeType.toString(),
3✔
141
        if (metadata != null) ...metadata,
1✔
142
      },
143
    );
144
  }
145

146
  /// Логирует warning с контекстом RPC операции
147
  Future<void> logRpcWarning({
×
148
    required String message,
149
    String? methodPath,
150
    int? streamId,
151
    RpcContext? context,
152
    Map<String, dynamic>? metadata,
153
  }) async {
154
    await warning(
×
155
      message,
156
      context: 'warning:${methodPath ?? 'unknown'}',
×
157
      rpcContext: context,
158
      data: {
×
159
        'method_path': methodPath,
×
160
        'stream_id': streamId,
×
161
        if (metadata != null) ...metadata,
×
162
      },
163
    );
164
  }
165

166
  // ============================================================================
167
  // Контекстные логгеры
168
  // ============================================================================
169

170
  /// Создает контекстный логгер с привязанным RpcContext
171
  ///
172
  /// Пример:
173
  /// ```dart
174
  /// final contextLogger = logger.withContext(rpcContext);
175
  /// contextLogger.info('Message'); // автоматически использует trace ID
176
  /// ```
177
  RpcLogger withContext(RpcContext context) {
×
178
    return RpcContextAwareLogger(this, context);
×
179
  }
180

181
  /// Создает контекстный логгер для операции
182
  ///
183
  /// Пример:
184
  /// ```dart
185
  /// final opLogger = logger.forOperation('CreateUser', context);
186
  /// opLogger.info('User created'); // включает operation в контекст
187
  /// ```
188
  RpcLogger forOperation(String operation, RpcContext? context) {
×
189
    if (context == null) {
190
      return child(operation);
×
191
    }
192

193
    final opContext = context.withAdditionalHeaders({
×
194
      'x-operation': operation,
195
    });
196

197
    return RpcContextAwareLogger(child(operation), opContext);
×
198
  }
199

200
  /// Создает контекстный логгер для stream
201
  ///
202
  /// Пример:
203
  /// ```dart
204
  /// final streamLogger = logger.forStream(streamId, context);
205
  /// streamLogger.debug('Processing stream'); // включает streamId в контекст
206
  /// ```
207
  RpcLogger forStream(int streamId, RpcContext? context) {
×
208
    if (context == null) {
209
      return child('stream.$streamId');
×
210
    }
211

212
    final streamContext = context.withAdditionalHeaders({
×
213
      'x-stream-id': streamId.toString(),
×
214
    });
215

216
    return RpcContextAwareLogger(child('stream.$streamId'), streamContext);
×
217
  }
218

219
  /// Создает контекстный логгер для метода
220
  ///
221
  /// Пример:
222
  /// ```dart
223
  /// final methodLogger = logger.forMethod('GetUser', context);
224
  /// methodLogger.info('Method called'); // включает method в контекст
225
  /// ```
226
  RpcLogger forMethod(String methodPath, RpcContext? context) {
×
227
    if (context == null) {
228
      return child(methodPath);
×
229
    }
230

231
    final methodContext = context.withAdditionalHeaders({
×
232
      'x-method-path': methodPath,
233
    });
234

235
    return RpcContextAwareLogger(child(methodPath), methodContext);
×
236
  }
237
}
238

239
/// Extension для удобной работы с RpcContext в логгерах
240
extension RpcContextLoggerExtensions on RpcContext {
241
  /// Создает логгер с привязанным контекстом
242
  ///
243
  /// Пример:
244
  /// ```dart
245
  /// final logger = context.createLogger('ServiceName');
246
  /// logger.info('Message'); // автоматически использует trace/request ID
247
  /// ```
248
  RpcLogger createLogger(String name) {
×
249
    return RpcLogger(name, context: this);
×
250
  }
251

252
  /// Создает дочерний логгер с обновленным контекстом
253
  ///
254
  /// Пример:
255
  /// ```dart
256
  /// final childLogger = context.createChildLogger(parentLogger, 'ChildService');
257
  /// ```
258
  RpcLogger createChildLogger(RpcLogger parentLogger, String childName) {
×
259
    return parentLogger.child(childName).withContext(this);
×
260
  }
261
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc