Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8832423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:31:30+00:00 2026-06-14T08:31:30+00:00

First off, I am new to Scala: I am writing a logging facility in

  • 0

First off, I am new to Scala:

I am writing a logging facility in Scala that will simply be a class that extends the Actor class. This way a user can just extend this class and the logging features will be available. Basically, I want to write to a log file every time an actor that extends this class sends or receives a message. For clarification every actor will have its own log file which can be collated later. I am taking a Lamport clocks style approach to ordering the events by having each Actor (who extends this class) have their own time variable that gets updated on a message send-receive and the actor will compare the current time variable (simply a positive integer) with the sender’s and update its time variable with the greater of the two.

For now I chose to make it a simple method like

sendMessage(recipient, message)

For sending messages. This will just log to the file that the actor is going to send a message to X.

Now, the part that I am stumped on is doing logging when receiving messages. When an actor gets a message I simply want to log this event in a format like

myLogFile.writeLine(self.ToString+": Got a message from "+X+" at time: "+messageSendTime+", processed the message at" +Math.max(myCurrTime+1, messageSendTime+1))

However I need to know who sent this message, unless I force upon the user to include this info (namely the sender’s name, time variable, etc) in the messages themselves, it gets hard(er). Is there any way to get the reference of the actual sender? I want this to work with remote actors as well. The only way I can think of is if I append to the act method that the user defines in his/her class with some extra case statements like:

def act {
    case => // the user's case statements
    ...

    //somehow I append these statements to the end for the Logger class's use
    case (LoggerClassRegisterInboundMessage, message, timeStamp)
        InboundMessagesMap.put(timeStamp, message)
}

By having this functionality I can do all the logging “behind the scenes” with these hidden messages being sent whenever the user sends a message. However this only works if the sender also uses the Logging facility. So a more general question is: is there a way in Scala to get the name/toString of a sender in Scala regardless of the sender’s class?

I’m actually OK with going with the assumption that every class that sends messages will extend the Logger class. So if anyone knows how to append to the act like or something similar to the above example I will be equally grateful!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:31:31+00:00Added an answer on June 14, 2026 at 8:31 am

    As it was said in the comments, Akka is the way to go. It’s so much more powerful than the current Scala Actor API which will become deprecated with 2.10 anyway.

    But, to attack your specific problem, you could create a trait for actors which support logging, in a way similar to this (I don’t know if this actually works, but you can try it):

    trait LoggingActor extends Actor {
        override def receive[R](pf: PartialFunction[Any, R]): R = {
            //we are appending to the partial function pf a case to handle messages with logging:
            val loggingPf = pf orElse {
                case (LoggerClassRegisterInboundMessage, message, timeStamp) => {
                    //do somthing with this log message.
                    message //returning the unwrapped result afterwards
                }
            }
            super.receive(loggingPf)
        }
    
        //overriding the send as well
        override def !(msg: Any): Unit {
            //Wrap it in a logging message
            super ! (LoggerClassRegisterInboundMessage, msg, getTimestamp())
        }
    }
    

    And you would create your actors with something like this:

    val myActor = new MyActor with LoggingActor
    

    Hope it helps !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off I am very new to Objective C and iPhone programming. Now that
First off, let me start by saying that I am totally new to working
First off, I would like to make clear, that I am SUPER NEW TO
First off I am new to this site and it is a big help,
First off I should forewarn you that I am a new grad(and EE at
First off my background: I'm new to Java coming over from Ruby. If that
First off, fairly new to JS but getting better :-) This question is similar
Okay, first off I am very, very new to java. For this project I
First off, I'm new to Scala. I'm trying to make a template parser in
First off, let me explain that I am well aware of what this exception

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.