Skip to content

Commit 434b616

Browse files
Merge pull request #5 from nein37/add_logtype_builder
Add logType method to PureeLogger
2 parents 7fe5e2f + 43b219c commit 434b616

File tree

4 files changed

+79
-75
lines changed

4 files changed

+79
-75
lines changed

demo/src/main/java/com/cookpad/puree/kotlin/demo/DemoApp.kt

+9-5
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,23 @@ class DemoApp : Application() {
4141
)
4242
.filter(
4343
AddTimeFilter(),
44-
ClickLog::class.java, MenuLog::class.java, PeriodicLog::class.java
44+
ClickLog::class.java, MenuLog::class.java
4545
)
4646
.output(
4747
LogcatOutput(),
48-
ClickLog::class.java, MenuLog::class.java, PeriodicLog::class.java
48+
ClickLog::class.java, MenuLog::class.java
4949
)
5050
.output(
5151
LogcatDebugBufferedOutput("logcat_debug"),
5252
ClickLog::class.java, MenuLog::class.java
5353
)
54-
.output(
55-
PurgeableLogcatWarningBufferedOutput("logcat_warning"),
56-
PeriodicLog::class.java
54+
.logType(
55+
logType = PeriodicLog::class.java,
56+
filters = listOf(AddTimeFilter()),
57+
outputs = listOf(
58+
LogcatOutput(),
59+
PurgeableLogcatWarningBufferedOutput("logcat_warning")
60+
)
5761
)
5862
.build()
5963
}

gradle/publishing.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: 'signing'
44
ext.publication = [:]
55
ext.publication.version = [
66
'major': '1',
7-
'minor': '2',
7+
'minor': '3',
88
'patch': '0',
99
]
1010
ext.publication.versionName = "${publication.version.major}.${publication.version.minor}.${publication.version.patch}"

puree/src/main/java/com/cookpad/puree/kotlin/PureeLogger.kt

+23-4
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ class PureeLogger private constructor(
5252
private val registeredLogs: Map<Class<out PureeLog>, Configuration>,
5353
private val bufferedOutputs: List<PureeBufferedOutput>
5454
) {
55-
private val scope: CoroutineScope = CoroutineScope(dispatcher + CoroutineExceptionHandler { _, throwable ->
56-
Log.e(TAG, "Exception thrown", throwable)
57-
})
55+
private val scope: CoroutineScope =
56+
CoroutineScope(dispatcher + CoroutineExceptionHandler { _, throwable ->
57+
Log.e(TAG, "Exception thrown", throwable)
58+
})
5859
private var isResumed: Boolean = false
5960

6061
init {
@@ -225,7 +226,7 @@ class PureeLogger private constructor(
225226
* [PureeOutput] should be registered and duplicates are ignored.
226227
* @param logTypes The log types of the objects that will be sent to the [PureeOutput].
227228
*/
228-
fun output(output: PureeOutput,vararg logTypes: Class<out PureeLog>): Builder {
229+
fun output(output: PureeOutput, vararg logTypes: Class<out PureeLog>): Builder {
229230
if (output is PureeBufferedOutput) {
230231
if (output.uniqueId in outputIds) {
231232
throw IllegalArgumentException("Cannot register another PureeBufferedOutput with uniqueId: ${output.uniqueId}.")
@@ -242,6 +243,24 @@ class PureeLogger private constructor(
242243
return this
243244
}
244245

246+
/**
247+
* Registers a log type and associate it with [PureeFilter] and [PureeOutput].
248+
*
249+
* @param logType The log type to be registered.
250+
* @param filters The [PureeFilter]s to use for the log type.
251+
* @param outputs The [PureeOutput]s that will emit this log type.
252+
*/
253+
fun logType(
254+
logType: Class<out PureeLog>,
255+
filters: List<PureeFilter>,
256+
outputs: List<PureeOutput>
257+
): Builder {
258+
filters.forEach { filter(it, logType) }
259+
outputs.forEach { output(it, logType) }
260+
261+
return this
262+
}
263+
245264
/**
246265
* Builds a [PureeLogger] object.
247266
*

puree/src/sharedTest/java/com/cookpad/puree/kotlin/PureeLoggerIntegrationTest.kt

+46-65
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,14 @@ class PureeLoggerIntegrationTest {
5858
).apply {
5959
dispatcher = coroutineDispatcher
6060
clock = this@FilterTests.clock
61-
filter(
62-
object : PureeFilter {
61+
logType(
62+
logType = SampleLog::class.java,
63+
filters = listOf(object : PureeFilter {
6364
override fun applyFilter(log: JSONObject): JSONObject = log.apply {
6465
put("filter1", "test_value")
6566
}
66-
},
67-
SampleLog::class.java
68-
)
69-
output(
70-
output,
71-
SampleLog::class.java
67+
}),
68+
outputs = listOf(output)
7269
)
7370
}.build()
7471

@@ -136,25 +133,21 @@ class PureeLoggerIntegrationTest {
136133
).apply {
137134
dispatcher = coroutineDispatcher
138135
clock = this@FilterTests.clock
139-
filter(
140-
object : PureeFilter {
141-
override fun applyFilter(log: JSONObject): JSONObject = log.apply {
142-
put("filter1", "test_value")
143-
}
144-
},
145-
SampleLog::class.java
146-
)
147-
filter(
148-
object : PureeFilter {
149-
override fun applyFilter(log: JSONObject): JSONObject = log.apply {
150-
put("filter2", "test_value")
136+
logType(
137+
logType = SampleLog::class.java,
138+
filters = listOf(
139+
object : PureeFilter {
140+
override fun applyFilter(log: JSONObject): JSONObject = log.apply {
141+
put("filter1", "test_value")
142+
}
143+
},
144+
object : PureeFilter {
145+
override fun applyFilter(log: JSONObject): JSONObject = log.apply {
146+
put("filter2", "test_value")
147+
}
151148
}
152-
},
153-
SampleLog::class.java
154-
)
155-
output(
156-
output,
157-
SampleLog::class.java
149+
),
150+
outputs = listOf(output)
158151
)
159152
}.build()
160153

@@ -183,25 +176,22 @@ class PureeLoggerIntegrationTest {
183176
).apply {
184177
dispatcher = coroutineDispatcher
185178
clock = this@FilterTests.clock
186-
filter(
187-
object : PureeFilter {
188-
override fun applyFilter(log: JSONObject): JSONObject = log.apply {
189-
put("filter1", "test_value")
190-
}
191-
},
192-
SampleLog::class.java
193-
)
194-
filter(
195-
object : PureeFilter {
196-
override fun applyFilter(log: JSONObject): JSONObject = JSONObject().apply {
197-
put("filter2", "test_value")
179+
logType(
180+
logType = SampleLog::class.java,
181+
filters = listOf(
182+
object : PureeFilter {
183+
override fun applyFilter(log: JSONObject): JSONObject = log.apply {
184+
put("filter1", "test_value")
185+
}
186+
},
187+
object : PureeFilter {
188+
override fun applyFilter(log: JSONObject): JSONObject =
189+
JSONObject().apply {
190+
put("filter2", "test_value")
191+
}
198192
}
199-
},
200-
SampleLog::class.java
201-
)
202-
output(
203-
output,
204-
SampleLog::class.java
193+
),
194+
outputs = listOf(output)
205195
)
206196
}.build()
207197

@@ -251,13 +241,10 @@ class PureeLoggerIntegrationTest {
251241
).apply {
252242
dispatcher = coroutineDispatcher
253243
clock = this@OutputTests.clock
254-
output(
255-
output1,
256-
SampleLog::class.java
257-
)
258-
output(
259-
output2,
260-
SampleLog::class.java
244+
logType(
245+
logType = SampleLog::class.java,
246+
filters = emptyList(),
247+
outputs = listOf(output1, output2)
261248
)
262249
}.build()
263250

@@ -315,13 +302,10 @@ class PureeLoggerIntegrationTest {
315302
).apply {
316303
dispatcher = coroutineDispatcher
317304
clock = this@OutputTests.clock
318-
output(
319-
outputEvery1s,
320-
SampleLog::class.java
321-
)
322-
output(
323-
outputEvery2s,
324-
SampleLog::class.java
305+
logType(
306+
logType = SampleLog::class.java,
307+
filters = emptyList(),
308+
outputs = listOf(outputEvery1s, outputEvery2s)
325309
)
326310
}.build()
327311

@@ -369,13 +353,10 @@ class PureeLoggerIntegrationTest {
369353
).apply {
370354
dispatcher = coroutineDispatcher
371355
clock = this@OutputTests.clock
372-
output(
373-
outputEvery5s,
374-
SampleLog::class.java
375-
)
376-
output(
377-
outputEvery10s,
378-
SampleLog::class.java
356+
logType(
357+
logType = SampleLog::class.java,
358+
filters = emptyList(),
359+
outputs = listOf(outputEvery5s, outputEvery10s)
379360
)
380361
}.build()
381362

0 commit comments

Comments
 (0)