Skip to content

Commit 0cbd9cc

Browse files
committed
Add session id to logs
1 parent 1d6aa65 commit 0cbd9cc

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

airframe/src/main/scala/wvlet/airframe/AirframeSession.scala

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
8080
}
8181

8282
override def newSharedChildSession(d: Design): Session = {
83-
trace(s"Creating a new shared child session with ${d}")
83+
trace(s"[${name}] Creating a new shared child session with ${d}")
8484
val childSession = new AirframeSession(
8585
parent = Some(this),
8686
sessionName, // Should we add suffixes for child sessions?
@@ -103,7 +103,7 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
103103
)
104104
// LifeCycleManager needs to know about the session
105105
l.setSession(childSession)
106-
trace(s"Creating a new child session ${childSession.name} with ${d}")
106+
trace(s"[${name}] Creating a new child session ${childSession.name} with ${d}")
107107
childSession
108108
}
109109

@@ -112,7 +112,7 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
112112
debug(s"[${name}] Initializing. Stage:${stage}")
113113
val production = stage == Stage.PRODUCTION
114114
if (production) {
115-
debug(s"Eagerly initializing singletons in production mode")
115+
debug(s"[${name}] Eagerly initializing singletons in production mode")
116116
}
117117
design.binding.collect {
118118
case s @ SingletonBinding(from, to, eager) if production || eager =>
@@ -124,21 +124,21 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
124124
}
125125

126126
private[airframe] def get[A](surface: Surface): A = {
127-
debug(s"Get dependency [${surface}]")
127+
debug(s"[${name}] Get dependency [${surface}]")
128128
getInstance(surface, this, create = false, List.empty).asInstanceOf[A]
129129
}
130130

131131
private[airframe] def getOrElse[A](surface: Surface, objectFactory: => A): A = {
132-
debug(s"Get dependency [${surface}] (or create with factory)")
132+
debug(s"[${name}] Get dependency [${surface}] (or create with factory)")
133133
getInstance(surface, this, create = false, List.empty, Some(() => objectFactory)).asInstanceOf[A]
134134
}
135135

136136
private[airframe] def createNewInstanceOf[A](surface: Surface): A = {
137-
debug(s"Create dependency [${surface}]")
137+
debug(s"[${name}] Create dependency [${surface}]")
138138
getInstance(surface, this, create = true, List.empty).asInstanceOf[A]
139139
}
140140
private[airframe] def createNewInstanceOf[A](surface: Surface, factory: => A): A = {
141-
debug(s"Create dependency [${surface}] (with factory)")
141+
debug(s"[${name}] Create dependency [${surface}] (with factory)")
142142
getInstance(surface, this, create = true, List.empty, Some(() => factory)).asInstanceOf[A]
143143
}
144144

@@ -147,7 +147,7 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
147147
* The other hooks (e.g., onStart, onShutdown) will be called in a separate step after the object is injected.
148148
*/
149149
private def registerInjectee(t: Surface, injectee: Any): AnyRef = {
150-
debug(s"Inject [${t}]: ${injectee}")
150+
debug(s"[${name}] Inject [${t}]: ${injectee}")
151151
observedTypes.getOrElseUpdate(t, System.currentTimeMillis())
152152
Try(lifeCycleManager.onInit(t, injectee.asInstanceOf[AnyRef])).recover {
153153
case e: Throwable =>
@@ -176,7 +176,7 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
176176
create: Boolean, // true for factory binding
177177
seen: List[Surface],
178178
defaultValue: Option[() => Any] = None): AnyRef = {
179-
trace(s"Search bindings for ${t}, dependencies:[${seen.mkString(" <- ")}]")
179+
trace(s"[${name}] Search bindings for ${t}, dependencies:[${seen.mkString(" <- ")}]")
180180
if (seen.contains(t)) {
181181
error(s"Found cyclic dependencies: ${seen}")
182182
throw new CYCLIC_DEPENDENCY(seen.toSet)
@@ -188,7 +188,7 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
188188
bindingTable.get(t) match {
189189
case None =>
190190
// If no binding is found in the current, traverse to the parent.
191-
trace(s"Search parent for ${t}")
191+
trace(s"[${name}] Search parent for ${t}")
192192
parent.flatMap { p =>
193193
p.findOwnerSessionOf(t).map { owner =>
194194
// Use the parent session only when some binding is found in the parent
@@ -199,21 +199,21 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
199199
val result =
200200
b match {
201201
case ClassBinding(from, to) =>
202-
trace(s"Found a class binding from ${from} to ${to}")
202+
trace(s"[${name}] Found a class binding from ${from} to ${to}")
203203
registerInjectee(from, contextSession.getInstance(to, contextSession, create, t :: seen))
204204
case sb @ SingletonBinding(from, to, eager) if from != to =>
205-
trace(s"Found a singleton binding: ${from} => ${to}")
205+
trace(s"[${name}] Found a singleton binding: ${from} => ${to}")
206206
singletonHolder.getOrElseUpdate(
207207
from,
208208
registerInjectee(from,
209209
contextSession.getInstance(to, contextSession, create, t :: seen, defaultValue)))
210210
case sb @ SingletonBinding(from, to, eager) if from == to =>
211-
trace(s"Found a singleton binding: ${from}")
211+
trace(s"[${name}] Found a singleton binding: ${from}")
212212
singletonHolder.getOrElseUpdate(
213213
from,
214214
registerInjectee(from, contextSession.buildInstance(to, contextSession, seen, defaultValue)))
215215
case p @ ProviderBinding(factory, provideSingleton, eager) =>
216-
trace(s"Found a provider for ${p.from}: ${p}")
216+
trace(s"[${name}] Found a provider for ${p.from}: ${p}")
217217
def buildWithProvider: Any = {
218218
val dependencies = for (d <- factory.dependencyTypes) yield {
219219
contextSession.getInstance(d, contextSession, false, t :: seen)
@@ -231,7 +231,7 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
231231

232232
val result =
233233
obj.getOrElse {
234-
trace(s"No binding is found for ${t}. Building the instance. create = ${create}")
234+
trace(s"[${name}] No binding is found for ${t}. Building the instance. create = ${create}")
235235
if (create) {
236236
// Create a new instance for bindFactory[X] or building X using its default value
237237
registerInjectee(t, contextSession.buildInstance(t, contextSession, seen, defaultValue))
@@ -252,18 +252,18 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
252252
defaultValue: Option[() => Any] = None): Any = {
253253
traitFactoryCache
254254
.get(t).map { f =>
255-
trace(s"Using a pre-registered trait factory for ${t}")
255+
trace(s"[${name}] Using a pre-registered trait factory for ${t}")
256256
f(this)
257257
}
258258
.orElse {
259259
// Use the provided object factory if exists
260260
defaultValue.map { f =>
261-
trace(s"Using the default value for ${t}")
261+
trace(s"[${name}] Using the default value for ${t}")
262262
f()
263263
}
264264
}
265265
.getOrElse {
266-
trace(s"No binding is found for ${t}")
266+
trace(s"[${name}] No binding is found for ${t}")
267267
buildInstance(t, contextSession, t :: seen)
268268
}
269269
}
@@ -272,7 +272,7 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
272272
* Create a new instance of the surface
273273
*/
274274
private def buildInstance(surface: Surface, contextSession: AirframeSession, seen: List[Surface]): Any = {
275-
trace(s"buildInstance ${surface}, dependencies:[${seen.mkString(" <- ")}]")
275+
trace(s"[${name}] buildInstance ${surface}, dependencies:[${seen.mkString(" <- ")}]")
276276
if (surface.isPrimitive) {
277277
// Cannot build Primitive types
278278
throw MISSING_DEPENDENCY(seen)
@@ -294,12 +294,12 @@ private[airframe] class AirframeSession(parent: Option[AirframeSession],
294294
case None =>
295295
val obj = traitFactoryCache.get(surface) match {
296296
case Some(factory) =>
297-
trace(s"Using pre-compiled factory for ${surface}")
297+
trace(s"[${name}] Using pre-compiled factory for ${surface}")
298298
factory.asInstanceOf[Session => Any](this)
299299
case None =>
300300
//buildWithReflection(t)
301301
warn(
302-
s"No binding nor the default constructor for ${surface} is found. " +
302+
s"[${name}] No binding nor the default constructor for ${surface} is found. " +
303303
s"Add bind[${surface}].toXXX to your design or dependencies:[${seen.mkString(" <- ")}]")
304304
throw MISSING_DEPENDENCY(seen)
305305
}

airframe/src/main/scala/wvlet/airframe/Design.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
package wvlet.airframe
1515

1616
import wvlet.airframe.Binder.Binding
17-
import wvlet.log.LogSupport
1817
import wvlet.airframe.surface.Surface
18+
import wvlet.log.LogSupport
1919

2020
import scala.language.experimental.macros
2121

@@ -83,7 +83,7 @@ case class Design(designOptions: DesignOptions, private[airframe] val binding: V
8383
}
8484

8585
def addBinding(b: Binding): Design = {
86-
debug(s"Add binding: $b")
86+
debug(s"Add a binding: $b")
8787
new Design(designOptions, Design.upsertBinding(binding, b))
8888
}
8989

airframe/src/main/scala/wvlet/airframe/LifeCycleManager.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,23 @@ class LifeCycleManager(private[airframe] val eventHandler: LifeCycleEventHandler
7979

8080
def addInitHook(h: LifeCycleHook): Unit = {
8181
if (initHookHolder.canRegistered(h)) {
82-
debug(s"Add init hook: ${h.surface}")
82+
debug(s"[${sessionName}] Add an init hook: ${h.surface}")
8383
h.execute
8484
} else {
85-
trace(s"${h.injectee} is already initialized")
85+
trace(s"[${sessionName}] ${h.injectee} is already initialized")
8686
}
8787
}
8888

8989
def addInjectHook(h: LifeCycleHook): Unit = {
90-
debug(s"Add inject hook: ${h.surface}")
90+
debug(s"[${sessionName}] Add an inject hook: ${h.surface}")
9191
// Run immediately
9292
h.execute
9393
}
9494

9595
def addStartHook(h: LifeCycleHook): Unit = {
9696
synchronized {
9797
if (startHookHolder.canRegistered(h)) {
98-
debug(s"Add start hook for ${h.surface}")
98+
debug(s"[${sessionName}] Add a start hook for ${h.surface}")
9999
val s = state.get
100100
if (s == STARTED) {
101101
// If a session is already started, run the start hook immediately
@@ -108,15 +108,15 @@ class LifeCycleManager(private[airframe] val eventHandler: LifeCycleEventHandler
108108
def addPreShutdownHook(h: LifeCycleHook): Unit = {
109109
synchronized {
110110
if (preShutdownHookHolder.canRegistered(h)) {
111-
debug(s"Add pre-shutdown hook for ${h.surface}")
111+
debug(s"[${sessionName}] Add a pre-shutdown hook for ${h.surface}")
112112
}
113113
}
114114
}
115115

116116
def addShutdownHook(h: LifeCycleHook): Unit = {
117117
synchronized {
118118
if (shutdownHookHolder.canRegistered(h)) {
119-
debug(s"Add shutdown hook for ${h.surface}")
119+
debug(s"[${sessionName}] Add a shutdown hook for ${h.surface}")
120120
}
121121
}
122122
}

0 commit comments

Comments
 (0)