Creating log files for ruby code

How can I put logs to a log file using ruby code in sketchup?

Another question, how to write to an existing log file?
I used this to create a new one -
log =‘C:\Users\Dell\Desktop\my_logs.log’, 2, 10241024)“I’m an info log”)
Now, I want to write to this file from another ruby module, what will happen if I use the same line again? (log =‘C:\Users\Dell\Desktop\my_logs.log’, 2, 1024


def test_log(path, logtext)
    file =,"w")
test_log('log.txt', 'mylogtext')

I’m looking for the use of Logger api

I tried using this -
require ‘logger’
logger =“my_log.txt”)“I’m an info log”)
logger.debug “A debug log”

but it’s not working

That one I never used…

…Is not the best way to describe the problem …:wink:

But please edit your post according this:

Try passing an absolute file path.

1 Like

… I agree. You cannot ever expect the current working directory to be where you think it should be. (Ie, people tend to write lazy code and don’t restore the working directory.)

1 Like

Looks like path was the only problem.
This is working -
log =‘C:\Users\Dell\Desktop\my_logs.log’, 2, 1024*1024)“I’m an info log”)

I guess your original code also worked… but you were unable to find your file. Try looking for it in the sketchup installation folder

yeah, quite possible.
I did look into the installation folder as well as the folder where i had my code, didn’t find the file anywhere

Can you help me with this part?

how to write to an existing log file?
I used this to create a new one -
log =‘C:\Users\Dell\Desktop\my_logs.log’, 2, 1024 x1024)“I’m an info log”)

Now, I want to write to this file from another ruby module, should I use the same line again? (log =‘C:\Users\Dell\Desktop\my_logs.log’, 2, 1024 x 1024))

The answer to this is a bit longer than what I am able to type in here on my cellphone.
Ideally you would create only one logger and share it with the rest of your code that needs it.

There are a few options: you make a logger module and call it from your other code. Alternatively you make a singleton logger class instance, or, you do some dependency injection and inject a logger instance into your other class instances, or, create one global service provider that is a module or singleton that distributes your logger, or, make the path a constant and just log how you do it now, without having the literal path defined in every piece of code, but by using the constant.

So many options, I go with dependency injection, but that requires a specific way your extension should be built from the ground up.

Files will get written to the current working directory.
After SketchUp gets done loading it will set it to the User’s “Documents” directory.
(But again, extensions can change it, so either use absolute paths, or use the block form of the Dir.chdir method, which restores the previous directory when the block exits.)

You can see what it is by typing at the console …


P.S. - AGAIN please read: [How to] Post correctly formatted and colorized code on the forum?

1 Like

How are your modules organized now ?

EDIT: I posted a couple of examples in a fresh example topic thread.
@kengey Feel free to add any nifty idea examples you like.

I believe the examples you have provided should help TS enough to get started.
Explaining dependency injection, why I do not like to use modules to hold state, or why I wrap everything in proxies… will just confuse TS.

1 Like