debug snippets

Enable remote debugging in weblogic

Tagged debug, weblogic  Languages bash

Sometimes it's necessary to debug because you can't write a test for it (e.g a legacy system). Just add the following parameters to the server startup and connect your preferred debugger into port 1044 (or whatever you choose the port to be). Works in weblogic, but should work in JBoss and other java based application servers too.

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044

Fixing "config.breakpoint_server has been deprecated and has no effect" when using Rails Edge

Tagged edge, debug, deprecated, rails  Languages ruby

You might get this error if you're using Rails Edge: config.breakpoint_server has been deprecated and has no effect:

To fix the error we need to remove the following from config/environments/development.rb:

config.breakpoint_server = true

Next install ruby-debug:

gem install ruby-debug

Then add this to the end of config/environments/development.rb:

require 'ruby-debug'

Next, start your server, and the error should be gone...

To debug your code just add a call to debugger:

class MySillySpace ...
  def create
    debugger # add this line

Now when you access the URL with your browser you'll have access to the debugger from the console window.

To learn how to use ruby-debug, read this tutorial written by the ruby-debug author.

How to add logging to CakePHP applications

Tagged php, cakephp, logging, debug  Languages php

Note that Pear's Logging package is a lot more flexible, so I recommend you use that instead of CakePHP's built-in logging.

Use this code to add a debug message to the CakePHP debug log:

$this->log("Upload action accessed", LOG_DEBUG);

Note that using something other than LOG_DEBUG will log the message as an error.

Run the code once and you should be able to see the message in $CAKE_APP/tmp/logs/debug.log.

Example of how to use Ruby's NET::HTTP

Tagged debug, net::http, http, ruby, example, https, timeout, proxy  Languages ruby

I always forget how to use NET:HTTP and examples online are of little help, so I use this code as a starting point when I work with NET:HTTP.

The code supports: * HTTP redirects * POST/GET * Headers * SSL/HTTPs * Proxies * Debugging HTTP traffic * HTTP connection and read timeout

require 'net/http'
require 'net/https'
require 'uri'

class HTTP
  class << self
    def get(url, options = {})
      execute(url, options)

    def post(url, options = { :method => :post })
      execute(url, options)

      def proxy
        http_proxy = ENV["http_proxy"]
        URI.parse(http_proxy) rescue nil

      def to_uri(url)
          if !url.kind_of?(URI) 

            url = URI.parse(url)
          raise URI::InvalidURIError, "Invalid url '#{url}'"

        if (url.class != URI::HTTP && url.class != URI::HTTPS)
          raise URI::InvalidURIError, "Invalid url '#{url}'"


      def execute(url, options = {})
        options = { :parameters => {}, :debug => false, 
                    :http_timeout => 60, :method => :get, 
                    :headers => {}, :redirect_count => 0, 
                    :max_redirects => 10 }.merge(options)

        url = to_uri(url)
        if proxy
          http = Net::HTTP::Proxy(, proxy.port).new(, url.port)
          http =, url.port)
        if url.scheme == 'https'
          http.use_ssl = true
          http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        http.open_timeout = http.read_timeout = options[:http_timeout]
        http.set_debug_output $stderr if options[:debug]
        request = case options[:method]
          when :post
            request =

        options[:headers].each { |key, value| request[key] = value }
        response = http.request(request)

        if response.kind_of?(Net::HTTPRedirection)      
          options[:redirect_count] += 1

          if options[:redirect_count] > options[:max_redirects]
            raise "Too many redirects (#{options[:redirect_count]}): #{url}" 

          redirect_url = redirect_url(response)

          if redirect_url.start_with?('/')
            url = to_uri("#{url.scheme}://#{}#{redirect_url}")

          response = execute(url, options)


      # From
      def redirect_url(response)
        if response['location'].nil?
          response.body.match(/<a href=\"([^>]+)\">/i)[1]

You can also find my Net:HTTP example on Github

How to debug Ansible variables

Tagged variables, ansible, debug, hostvars  Languages bash, yaml

Print all variables for all hosts from the command line:

 $ ansible -i inventory/local -m debug -a "var=hostvars" all

Replace hostvars with any of the following to print:

  • ansible_locals
  • groups
  • group_names
  • environment
  • vars
  • ansible_sucks

Print all variables for all hosts from a playbook:

- hosts: all
    -  debug:
        var: hostvars[inventory_hostname]
        # -vvv to debug !!!!
        # verbosity: 4

Print all variables:

- name: print ansible_local
  debug: var=ansible_local

Exploring log wrappers for Go

Tagged go, log, logging, debug, info, wrapper  Languages go

Version 1

This is a simple log wrapper for go’s built in log package. Put the following code in a file named log.go:

package main

import (
    golog "log"

// Use ioutil.Discard instead of f to send log to '/dev/null'
type Logger struct {
    Info     *golog.Logger
    Error    *golog.Logger
    Debug    *golog.Logger
    FatalLog *golog.Logger
func (logger Logger) Fatal(format string, args ...interface{}) {
    logger.FatalLog.Printf(format, args...)
    golog.Fatalf(format, args...)

var logFile *os.File
var log Logger

func init() {
    fileName := os.Getenv("LOG")
    if len(fileName) > 0 {
        var err error
        logFile, err = os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
        if err != nil {
            golog.Fatalf("error opening file: %v", err)
    } else {
        logFile = os.Stdout
    log.Info = golog.New(logFile, "INFO  ", golog.LstdFlags)
    log.Error = golog.New(logFile, "ERROR ", golog.LstdFlags)
    log.Debug = golog.New(logFile, "DEBUG ", golog.LstdFlags)
    log.FatalLog = golog.New(logFile, "FATAL ", golog.LstdFlags)


log.Info.Printf("Notifications %v", notifications)
log.Debug.Printf("Notifications %v", notifications)

Version 2

To remove the call to Printf you could do this:

type Log struct {
    //Info     *golog.Logger
    Info     func(string, ...interface{})

func init() {
    log.Info = golog.New(logFile, "INFO  ", golog.LstdFlags).Printf

Now you can simply call log.Info:

log.Info("Notifications %v", notifications)
log.Debug("Notifications %v", notifications)


Version 3

An even simpler log wrapper:

package main

import (

var debug bool

func init() {
    debug = os.Getenv("DEBUG") != ""

func Log(msg string, vars ...interface{}) {
    log.Printf(strings.Join([]string{"[INFO ]", msg}, " "), vars...)

func Debug(msg string, vars ...interface{}) {
    if debug {
        log.Printf(strings.Join([]string{"[DEBUG]", msg}, " "), vars...)